![](/media/cache/group/logo_thTzkVQ.jpg.50x50_q85.jpg)
NuMaker NuWicam sample on NUC472
Dependencies: Modbus nvt_rs485
NuWicam for NuMaker-PFM-NUC472 board. Please refer document before executing it. More details, please refer https://github.com/OpenNuvoton/NuMaker_NuWicam_Samples/
main.cpp@0:084b94d2c1b5, 2016-08-09 (annotated)
- Committer:
- wclin
- Date:
- Tue Aug 09 08:21:49 2016 +0000
- Revision:
- 0:084b94d2c1b5
- Child:
- 2:60deb475f831
first version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wclin | 0:084b94d2c1b5 | 1 | /* |
wclin | 0:084b94d2c1b5 | 2 | * The program is a sample code. |
wclin | 0:084b94d2c1b5 | 3 | * It needs run with NuMaker NuWicam board. |
wclin | 0:084b94d2c1b5 | 4 | */ |
wclin | 0:084b94d2c1b5 | 5 | |
wclin | 0:084b94d2c1b5 | 6 | /* ----------------------- System includes --------------------------------*/ |
wclin | 0:084b94d2c1b5 | 7 | #include "mbed.h" |
wclin | 0:084b94d2c1b5 | 8 | #include "rtos.h" |
wclin | 0:084b94d2c1b5 | 9 | /*----------------------- Modbus includes ----------------------------------*/ |
wclin | 0:084b94d2c1b5 | 10 | #include "mb.h" |
wclin | 0:084b94d2c1b5 | 11 | #include "mbport.h" |
wclin | 0:084b94d2c1b5 | 12 | |
wclin | 0:084b94d2c1b5 | 13 | /* ----------------------- Defines ------------------------------------------*/ |
wclin | 0:084b94d2c1b5 | 14 | // Sharing buffer index |
wclin | 0:084b94d2c1b5 | 15 | enum { |
wclin | 0:084b94d2c1b5 | 16 | eData_MBInCounter, |
wclin | 0:084b94d2c1b5 | 17 | eData_MBOutCounter, |
wclin | 0:084b94d2c1b5 | 18 | eData_MBError, |
wclin | 0:084b94d2c1b5 | 19 | eData_DI, |
wclin | 0:084b94d2c1b5 | 20 | eData_DO, |
wclin | 0:084b94d2c1b5 | 21 | eData_RGB, |
wclin | 0:084b94d2c1b5 | 22 | eData_MBResistorVar, |
wclin | 0:084b94d2c1b5 | 23 | eData_TemperatureSensor, |
wclin | 0:084b94d2c1b5 | 24 | eData_Cnt |
wclin | 0:084b94d2c1b5 | 25 | } E_DATA_TYPE; |
wclin | 0:084b94d2c1b5 | 26 | |
wclin | 0:084b94d2c1b5 | 27 | #define REG_INPUT_START 1 |
wclin | 0:084b94d2c1b5 | 28 | #define REG_INPUT_NREGS eData_Cnt |
wclin | 0:084b94d2c1b5 | 29 | #define SLAVE_ID 0x01 |
wclin | 0:084b94d2c1b5 | 30 | /* ----------------------- Static variables ---------------------------------*/ |
wclin | 0:084b94d2c1b5 | 31 | static USHORT usRegInputStart = REG_INPUT_START; |
wclin | 0:084b94d2c1b5 | 32 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; |
wclin | 0:084b94d2c1b5 | 33 | |
wclin | 0:084b94d2c1b5 | 34 | DigitalOut led1(LED1); // For temperature worker. |
wclin | 0:084b94d2c1b5 | 35 | DigitalOut led2(LED2); // For Modbus worker. |
wclin | 0:084b94d2c1b5 | 36 | DigitalOut led3(LED3); // For Holder CB |
wclin | 0:084b94d2c1b5 | 37 | |
wclin | 0:084b94d2c1b5 | 38 | AnalogIn LM35(A0); |
wclin | 0:084b94d2c1b5 | 39 | |
wclin | 0:084b94d2c1b5 | 40 | #define DEF_LED_NUM 6 |
wclin | 0:084b94d2c1b5 | 41 | DigitalOut LED[DEF_LED_NUM] = { PF_9, PF_10, PC_10, PC_11, PA_10, PA_9 } ; |
wclin | 0:084b94d2c1b5 | 42 | |
wclin | 0:084b94d2c1b5 | 43 | |
wclin | 0:084b94d2c1b5 | 44 | void light_leds() |
wclin | 0:084b94d2c1b5 | 45 | { |
wclin | 0:084b94d2c1b5 | 46 | int i=0; |
wclin | 0:084b94d2c1b5 | 47 | USHORT usOutValue = ~usRegInputBuf[eData_DO]; |
wclin | 0:084b94d2c1b5 | 48 | |
wclin | 0:084b94d2c1b5 | 49 | //printf("[%s %d] 0x%08X\r\n", __func__, __LINE__, usOutValue ); |
wclin | 0:084b94d2c1b5 | 50 | |
wclin | 0:084b94d2c1b5 | 51 | for ( i=0; i<DEF_LED_NUM ; i++) |
wclin | 0:084b94d2c1b5 | 52 | LED[i].write( (usOutValue&(0x1<<i)) >> i ); |
wclin | 0:084b94d2c1b5 | 53 | } |
wclin | 0:084b94d2c1b5 | 54 | |
wclin | 0:084b94d2c1b5 | 55 | void get_temp(void) |
wclin | 0:084b94d2c1b5 | 56 | { |
wclin | 0:084b94d2c1b5 | 57 | float tempC, a[10], avg; |
wclin | 0:084b94d2c1b5 | 58 | int i; |
wclin | 0:084b94d2c1b5 | 59 | #define DEF_ADC_READTIMES 10 |
wclin | 0:084b94d2c1b5 | 60 | avg=0; |
wclin | 0:084b94d2c1b5 | 61 | for(i=0;i<DEF_ADC_READTIMES;i++) |
wclin | 0:084b94d2c1b5 | 62 | { |
wclin | 0:084b94d2c1b5 | 63 | a[i] = LM35.read(); |
wclin | 0:084b94d2c1b5 | 64 | Thread::wait(1); |
wclin | 0:084b94d2c1b5 | 65 | } |
wclin | 0:084b94d2c1b5 | 66 | |
wclin | 0:084b94d2c1b5 | 67 | for ( i=0; i<DEF_ADC_READTIMES; i++ ) |
wclin | 0:084b94d2c1b5 | 68 | avg += a[i]; |
wclin | 0:084b94d2c1b5 | 69 | |
wclin | 0:084b94d2c1b5 | 70 | avg /= DEF_ADC_READTIMES; |
wclin | 0:084b94d2c1b5 | 71 | |
wclin | 0:084b94d2c1b5 | 72 | //tempC=(avg*3.685503686*100); |
wclin | 0:084b94d2c1b5 | 73 | tempC=3.3*avg*100; |
wclin | 0:084b94d2c1b5 | 74 | usRegInputBuf[eData_TemperatureSensor] = (USHORT)tempC; |
wclin | 0:084b94d2c1b5 | 75 | |
wclin | 0:084b94d2c1b5 | 76 | printf("[%s %d] %d\r\n", __func__, __LINE__, usRegInputBuf[eData_TemperatureSensor] ); |
wclin | 0:084b94d2c1b5 | 77 | } |
wclin | 0:084b94d2c1b5 | 78 | |
wclin | 0:084b94d2c1b5 | 79 | void worker_get_temperature(void const *args) |
wclin | 0:084b94d2c1b5 | 80 | { |
wclin | 0:084b94d2c1b5 | 81 | // Poll temperature sensor per 1 second. |
wclin | 0:084b94d2c1b5 | 82 | while (true) { |
wclin | 0:084b94d2c1b5 | 83 | get_temp(); |
wclin | 0:084b94d2c1b5 | 84 | led1 = !led1; |
wclin | 0:084b94d2c1b5 | 85 | Thread::wait(1000); |
wclin | 0:084b94d2c1b5 | 86 | } |
wclin | 0:084b94d2c1b5 | 87 | } |
wclin | 0:084b94d2c1b5 | 88 | |
wclin | 0:084b94d2c1b5 | 89 | void worker_uart(void const *args) |
wclin | 0:084b94d2c1b5 | 90 | { |
wclin | 0:084b94d2c1b5 | 91 | // For UART-SERIAL Tx/Rx Service. |
wclin | 0:084b94d2c1b5 | 92 | while (true) { |
wclin | 0:084b94d2c1b5 | 93 | xMBPortSerialPolling(); |
wclin | 0:084b94d2c1b5 | 94 | } |
wclin | 0:084b94d2c1b5 | 95 | } |
wclin | 0:084b94d2c1b5 | 96 | |
wclin | 0:084b94d2c1b5 | 97 | /* ----------------------- Start implementation -----------------------------*/ |
wclin | 0:084b94d2c1b5 | 98 | int |
wclin | 0:084b94d2c1b5 | 99 | main( void ) |
wclin | 0:084b94d2c1b5 | 100 | { |
wclin | 0:084b94d2c1b5 | 101 | eMBErrorCode eStatus; |
wclin | 0:084b94d2c1b5 | 102 | Thread temperature_thread(worker_get_temperature); |
wclin | 0:084b94d2c1b5 | 103 | Thread uart_thread(worker_uart); |
wclin | 0:084b94d2c1b5 | 104 | |
wclin | 0:084b94d2c1b5 | 105 | // Initialise some registers |
wclin | 0:084b94d2c1b5 | 106 | for (int i=0; i<REG_INPUT_NREGS; i++) |
wclin | 0:084b94d2c1b5 | 107 | usRegInputBuf[i] = 0x0; |
wclin | 0:084b94d2c1b5 | 108 | |
wclin | 0:084b94d2c1b5 | 109 | /* Enable the Modbus Protocol Stack. */ |
wclin | 0:084b94d2c1b5 | 110 | if ( (eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR ) |
wclin | 0:084b94d2c1b5 | 111 | goto FAIL_MB; |
wclin | 0:084b94d2c1b5 | 112 | else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR ) |
wclin | 0:084b94d2c1b5 | 113 | goto FAIL_MB_1; |
wclin | 0:084b94d2c1b5 | 114 | else { |
wclin | 0:084b94d2c1b5 | 115 | for( ;; ) |
wclin | 0:084b94d2c1b5 | 116 | { |
wclin | 0:084b94d2c1b5 | 117 | if ( eMBPoll( ) != MB_ENOERR ) break; |
wclin | 0:084b94d2c1b5 | 118 | Thread::wait(1); |
wclin | 0:084b94d2c1b5 | 119 | } |
wclin | 0:084b94d2c1b5 | 120 | } |
wclin | 0:084b94d2c1b5 | 121 | |
wclin | 0:084b94d2c1b5 | 122 | FAIL_MB_1: |
wclin | 0:084b94d2c1b5 | 123 | eMBClose(); |
wclin | 0:084b94d2c1b5 | 124 | |
wclin | 0:084b94d2c1b5 | 125 | FAIL_MB: |
wclin | 0:084b94d2c1b5 | 126 | for( ;; ) |
wclin | 0:084b94d2c1b5 | 127 | { |
wclin | 0:084b94d2c1b5 | 128 | led2 = !led2; |
wclin | 0:084b94d2c1b5 | 129 | Thread::wait(200); |
wclin | 0:084b94d2c1b5 | 130 | } |
wclin | 0:084b94d2c1b5 | 131 | |
wclin | 0:084b94d2c1b5 | 132 | } |
wclin | 0:084b94d2c1b5 | 133 | |
wclin | 0:084b94d2c1b5 | 134 | |
wclin | 0:084b94d2c1b5 | 135 | |
wclin | 0:084b94d2c1b5 | 136 | |
wclin | 0:084b94d2c1b5 | 137 | |
wclin | 0:084b94d2c1b5 | 138 | |
wclin | 0:084b94d2c1b5 | 139 | |
wclin | 0:084b94d2c1b5 | 140 | |
wclin | 0:084b94d2c1b5 | 141 | |
wclin | 0:084b94d2c1b5 | 142 | |
wclin | 0:084b94d2c1b5 | 143 | |
wclin | 0:084b94d2c1b5 | 144 | |
wclin | 0:084b94d2c1b5 | 145 | |
wclin | 0:084b94d2c1b5 | 146 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 147 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
wclin | 0:084b94d2c1b5 | 148 | { |
wclin | 0:084b94d2c1b5 | 149 | eMBErrorCode eStatus = MB_ENOERR; |
wclin | 0:084b94d2c1b5 | 150 | int iRegIndex; |
wclin | 0:084b94d2c1b5 | 151 | |
wclin | 0:084b94d2c1b5 | 152 | //printf("[%s %d] %d %d\r\n", __func__, __LINE__, usAddress, REG_INPUT_START); |
wclin | 0:084b94d2c1b5 | 153 | |
wclin | 0:084b94d2c1b5 | 154 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 155 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 156 | { |
wclin | 0:084b94d2c1b5 | 157 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 158 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 159 | { |
wclin | 0:084b94d2c1b5 | 160 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 161 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
wclin | 0:084b94d2c1b5 | 162 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 163 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
wclin | 0:084b94d2c1b5 | 164 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 165 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 166 | } |
wclin | 0:084b94d2c1b5 | 167 | } |
wclin | 0:084b94d2c1b5 | 168 | else |
wclin | 0:084b94d2c1b5 | 169 | { |
wclin | 0:084b94d2c1b5 | 170 | eStatus = MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 171 | } |
wclin | 0:084b94d2c1b5 | 172 | |
wclin | 0:084b94d2c1b5 | 173 | return eStatus; |
wclin | 0:084b94d2c1b5 | 174 | } |
wclin | 0:084b94d2c1b5 | 175 | |
wclin | 0:084b94d2c1b5 | 176 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 177 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) |
wclin | 0:084b94d2c1b5 | 178 | { |
wclin | 0:084b94d2c1b5 | 179 | eMBErrorCode eStatus = MB_ENOERR; |
wclin | 0:084b94d2c1b5 | 180 | int iRegIndex; |
wclin | 0:084b94d2c1b5 | 181 | |
wclin | 0:084b94d2c1b5 | 182 | //printf("[%s %d] Mode=%d usAddress=%d(%d) %d\r\n", __func__, __LINE__, eMode, usAddress, REG_INPUT_START, usNRegs); |
wclin | 0:084b94d2c1b5 | 183 | |
wclin | 0:084b94d2c1b5 | 184 | usRegInputBuf[eData_MBInCounter]++; |
wclin | 0:084b94d2c1b5 | 185 | usRegInputBuf[eData_MBOutCounter]++; |
wclin | 0:084b94d2c1b5 | 186 | |
wclin | 0:084b94d2c1b5 | 187 | if (eMode == MB_REG_READ) |
wclin | 0:084b94d2c1b5 | 188 | { |
wclin | 0:084b94d2c1b5 | 189 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 190 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 191 | { |
wclin | 0:084b94d2c1b5 | 192 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 193 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 194 | { |
wclin | 0:084b94d2c1b5 | 195 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 196 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
wclin | 0:084b94d2c1b5 | 197 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 198 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
wclin | 0:084b94d2c1b5 | 199 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 200 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 201 | } |
wclin | 0:084b94d2c1b5 | 202 | } |
wclin | 0:084b94d2c1b5 | 203 | } |
wclin | 0:084b94d2c1b5 | 204 | |
wclin | 0:084b94d2c1b5 | 205 | if (eMode == MB_REG_WRITE) |
wclin | 0:084b94d2c1b5 | 206 | { |
wclin | 0:084b94d2c1b5 | 207 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 208 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 209 | { |
wclin | 0:084b94d2c1b5 | 210 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 211 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 212 | { |
wclin | 0:084b94d2c1b5 | 213 | usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
wclin | 0:084b94d2c1b5 | 214 | pucRegBuffer+=2; |
wclin | 0:084b94d2c1b5 | 215 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 216 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 217 | } |
wclin | 0:084b94d2c1b5 | 218 | light_leds(); // Control LEDs |
wclin | 0:084b94d2c1b5 | 219 | } |
wclin | 0:084b94d2c1b5 | 220 | } |
wclin | 0:084b94d2c1b5 | 221 | |
wclin | 0:084b94d2c1b5 | 222 | led3=!led3; |
wclin | 0:084b94d2c1b5 | 223 | |
wclin | 0:084b94d2c1b5 | 224 | return eStatus; |
wclin | 0:084b94d2c1b5 | 225 | } |
wclin | 0:084b94d2c1b5 | 226 | |
wclin | 0:084b94d2c1b5 | 227 | |
wclin | 0:084b94d2c1b5 | 228 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 229 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
wclin | 0:084b94d2c1b5 | 230 | eMBRegisterMode eMode ) |
wclin | 0:084b94d2c1b5 | 231 | { |
wclin | 0:084b94d2c1b5 | 232 | return MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 233 | } |
wclin | 0:084b94d2c1b5 | 234 | |
wclin | 0:084b94d2c1b5 | 235 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 236 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
wclin | 0:084b94d2c1b5 | 237 | { |
wclin | 0:084b94d2c1b5 | 238 | return MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 239 | } |