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/
Diff: main.cpp
- Revision:
- 5:a1dd75c39f6a
- Parent:
- 3:489a1d39f6ac
- Child:
- 9:a2e3c8955dda
--- a/main.cpp Tue Sep 20 06:18:35 2016 +0000 +++ b/main.cpp Tue Sep 20 06:41:11 2016 +0000 @@ -1,6 +1,6 @@ /* * The program is a sample code. -* It needs run with some NuMaker-PFM-NUC472 boards. +* It needs run with NuMaker NuWicam board. */ /* ----------------------- System includes --------------------------------*/ @@ -17,12 +17,16 @@ eData_MBOutCounter, eData_MBError, eData_DI, - eData_DATA, + eData_DO, + eData_RGB, + eData_MBResistorVar, + eData_TemperatureSensor, eData_Cnt } E_DATA_TYPE; #define REG_INPUT_START 1 #define REG_INPUT_NREGS eData_Cnt +#define SLAVE_ID 0x01 /* ----------------------- Static variables ---------------------------------*/ static USHORT usRegInputStart = REG_INPUT_START; static USHORT usRegInputBuf[REG_INPUT_NREGS]; @@ -31,51 +35,76 @@ DigitalOut led2(LED2); // For Modbus worker. DigitalOut led3(LED3); // For Holder CB -#define DEF_PIN_NUM 6 -DigitalIn DipSwitch[DEF_PIN_NUM] = { PG_1, PG_2, PF_9, PF_10, PC_10, PC_11 } ; +AnalogIn LM35(A0); -unsigned short GetValueOnDipSwitch() +#define DEF_LED_NUM 6 +DigitalOut LED[DEF_LED_NUM] = { PF_9, PF_10, PC_10, PC_11, PA_10, PA_9 } ; + + +void light_leds() { int i=0; - unsigned short usDipValue = 0x0; - for ( i=0; i<DEF_PIN_NUM ; i++) - usDipValue |= DipSwitch[i].read() << i; - usDipValue = (~usDipValue) & 0x003F; - return usDipValue; + USHORT usOutValue = ~usRegInputBuf[eData_DO]; + for ( i=0; i<DEF_LED_NUM ; i++) + LED[i].write( (usOutValue&(0x1<<i)) >> i ); } +void get_temp(void) +{ + float tempC, a[10], avg; + int i; + #define DEF_ADC_READTIMES 10 + avg=0; + for(i=0;i<DEF_ADC_READTIMES;i++) + { + a[i] = LM35.read(); + Thread::wait(1); + } + + for ( i=0; i<DEF_ADC_READTIMES; i++ ) + avg += a[i]; + + avg /= DEF_ADC_READTIMES; + tempC=(avg*3.685503686*100); + usRegInputBuf[eData_TemperatureSensor] = (USHORT)tempC; + //printf("[%s %d] %f %d\r\n", __func__, __LINE__, avg, usRegInputBuf[eData_TemperatureSensor] ); +} + +void worker_get_temperature(void const *args) +{ + // Poll temperature sensor per 1 second. + while (true) { + get_temp(); + led1 = !led1; + Thread::wait(1000); + } +} + +#if 0 void worker_uart(void const *args) { - int counter=0; // For UART-SERIAL Tx/Rx Service. while (true) - { - //xMBPortSerialPolling(); - if ( counter > 10000 ) - { - led2 = !led2; - counter=0; - } - counter++; - } + xMBPortSerialPolling(); } +#endif /* ----------------------- Start implementation -----------------------------*/ int main( void ) { eMBErrorCode eStatus; + Thread temperature_thread(worker_get_temperature); //Thread uart_thread(worker_uart); - unsigned short usSlaveID=GetValueOnDipSwitch(); // Initialise some registers for (int i=0; i<REG_INPUT_NREGS; i++) usRegInputBuf[i] = 0x0; - - printf("We will set modbus slave ID-%d(0x%x) for the device.\r\n", usSlaveID, usSlaveID ); + light_leds(); // Control LEDs + /* Enable the Modbus Protocol Stack. */ - if ( (eStatus = eMBInit( MB_RTU, usSlaveID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR ) + if ( (eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR ) goto FAIL_MB; else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR ) goto FAIL_MB_1; @@ -101,6 +130,15 @@ + + + + + + + + + eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) { @@ -140,8 +178,6 @@ if (eMode == MB_REG_READ) { - usRegInputBuf[eData_DI] = GetValueOnDipSwitch(); - if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) { @@ -171,6 +207,7 @@ iRegIndex++; usNRegs--; } + light_leds(); // Control LEDs } }