// PIC16F1827 Wireless Voltage Monitor 2015.2.21 // ===> AQM0802A Akizuki // UART ===> TWE-Lite SMD #include<16F1827.h> #device ADC=10 #fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,BROWNOUT // #use delay(clock=16000000) #use i2c(MASTER,SDA=PIN_B1,SCL=PIN_B4,FAST,NOFORCE_SW) #use RS232(PARITY=N, BAUD=38400,XMIT=PIN_B5, RCV=PIN_B1) // ACM1602 #define LCD_ADD 0xA0 // ACM1602 Slave Address // AQM0802A #define LCD_ADD_AQM 0x7C // AQM0802 Slave Address // AQM W/R Mode val #define LCD_CMD_AQM 0x80 // Instruction Write Mode #define LCD_DAT_AQM 0xC0 // Data Write Mode #define line_1_AQM 0x80 // first line #define line_2_AQM 0xC0 // second line = 0x80 + 0x40 void LCD_com_AQM(unsigned char cmd); void LCD_Clear_AQM(); void LCD_Setline_AQM(unsigned char line); void LCD_init_AQM(); void LCD_data_AQM(unsigned char data); void LCD_space_AQM(int8 n); ///////////////////////////////////////////////// #define MAX 10 // Ave. Number void ADT_init(); void ADT_data(unsigned char data); void ADT_cmd(unsigned char data); ///////////////////////////////////////////////// static int16 d1,d2,d3,d4,d5; void disp_val(int16 x); static int16 t_wait; static int8 tglsw; // Efect only this position ?? // ========================================== void main(){ int8 pntr; int8 counter; int16 MIN_AD; int16 ADC; int16 AD[MAX]; int16 pct; char title[9] = "Rec.Volt"; LCD_init_AQM(); LCD_Clear_AQM(); // Infuluence to ACM1602 LCD_Setline_AQM(line_1_AQM); for(counter=0;counter<=8;counter++){ LCD_data_AQM(title[counter]); } delay_ms(600); t_wait = 3; setup_adc_ports(0x80); setup_adc(ADC_CLOCK_INTERNAL); delay_ms(1000); tglsw = 0; MIN_AD = 6*0x3ff; while(1) { if( tglsw++ & 0x01){ output_high(PIN_B3); } else{ output_low(PIN_B3); } pntr = 0; while(pntr <= MAX){ set_adc_channel(0); delay_us(50); //ADC = 1.9995*3.265*read_adc(); // Small Type AD[pntr++] = 1.9995*2.923*read_adc(); // Wireless Monitoring } ADC = 0; pntr = 0; while(pntr <= MAX){ ADC += AD[pntr++]; } ADC /= max; // to Average if( ADC < MIN_AD ){ MIN_AD = ADC; } LCD_Setline_AQM(line_1_AQM); disp_val(ADC); LCD_space_AQM(2); LCD_Setline_AQM(line_2_AQM); disp_val(MIN_AD); if( ADC > 4160 ){ pct = 99; } else{ if( ADC >= 3900 ){ pct = ADC*0.14 - 484; } if( ADC < 3900 ){ if( ADC < 3775){ pct = 0; } else{ pct = ADC*0.233 - 852; } } } d5 = pct % 10 + 0x30; pct /= 10; d4 = pct % 10 + 0x30; pct /= 10; d3 = pct % 10 + 0x30; pct /= 10; d2 = pct % 10 + 0x30; pct /= 10; d1 = pct % 10 + 0x30; printf("%1c",0x20); delay_ms(5); printf("%1c",d4); delay_ms(5); printf("%1c",d5); delay_ms(5); printf("%1c",0x25); delay_ms(5); // '%' // 0x3b == ';' As End of line printf("%1c",0x3b); delay_ms(5); // ";" printf("%1c",0x3b); delay_ms(5); printf("%1c",0x0d); delay_ms(5); // C/R } } // AQM0802 // void LCD_com_AQM(unsigned char cmd){ int16 u_t=30; i2c_start(); delay_ms(1); i2c_write(LCD_ADD_AQM); delay_us(u_t); // Slave Address i2c_write(LCD_CMD_AQM); delay_us(u_t); // Command Mode i2c_write(cmd); delay_us(u_t); // Send Command i2c_stop(); delay_ms(1); } void LCD_Clear_AQM(){ LCD_com_AQM(0x01); delay_ms(2); } void LCD_Setline_AQM(unsigned char line) { LCD_com_AQM(line); } void LCD_init_AQM(){ delay_ms(60); LCD_com_AQM(0x38); // Function set delay_us(30); LCD_com_AQM(0x39); // Function set delay_us(30); LCD_com_AQM(0x14); // Internal OSC frequency delay_us(30); LCD_com_AQM(0x70); // Contrast delay_us(30); LCD_com_AQM(0x56); // Power/ICON/Contrast Control delay_us(30); LCD_com_AQM(0x6c); // Follow Control delay_ms(300); LCD_com_AQM(0x38); // Function set delay_us(30); LCD_com_AQM(0x0c); // Display ON/OFF Control delay_us(30); LCD_com_AQM(0x01); // Clear Display delay_ms(2); } void LCD_data_AQM(unsigned char data){ int16 u_t=26; i2c_start(); delay_ms(1); i2c_write(LCD_ADD_AQM); delay_us(u_t); // Slave Address i2c_write(LCD_DAT_AQM); delay_us(u_t); // Data Out Code i2c_write(data); delay_us(u_t); // Send Command i2c_stop(); delay_ms(1); } void LCD_space_AQM(int8 n){ int8 cnt; cnt = 0; while(cnt++ < n){ LCD_data_AQM(' '); } } void disp_val(int16 x){ // Disp Digits int8 sw; if( x == 0xFFFF){ printf("Ox FFFF"); delay_ms(t_wait); } else{ if( x & 0x8000){ // Minus ? printf("-"); delay_ms(t_wait); x = ( x ^ 0xFFFF)+1; } else{ printf(" "); delay_ms(t_wait); } d5 = x % 10 + 0x30; x /= 10; d4 = x % 10 + 0x30; x /= 10; d3 = x % 10 + 0x30; x /= 10; d2 = x % 10 + 0x30; x /= 10; d1 = x % 10 + 0x30; sw = 0; if(d1 == 0x30){ d1 = 0x20; } else{ sw = 1; } if( (d2 == 0x30)&&(sw == 0)){ d2 = 0x20; } else{ sw = 1; } if((d3 == 0x30)&&(sw == 0)){ d3 = 0x20; } else{ sw = 1; } if((d4 == 0x30)&&(sw == 0)){ d4 = 0x20; } //printf("%1c",d1); delay_ms(t_wait); printf("%1c",d2); delay_ms(t_wait); printf("%1c",0x2e); delay_ms(t_wait); // 0x2e == '.' printf("%1c",d3); delay_ms(t_wait); printf("%1c",d4); delay_ms(t_wait); printf("%1c",d5); delay_ms(t_wait); LCD_data_AQM(d1); LCD_data_AQM(d2); LCD_data_AQM('.'); LCD_data_AQM(d3); LCD_data_AQM(d4); LCD_data_AQM(d5); } }