/* Solar.c sample code for solar integrator built with 8051SBC+onboard 12bit ADC and DC amplifier 19 Jan 2005 Written by W.SIRICHOTE, kswichit@kmitl.ac.th */ #include <8051io.h> /* include i/o header file */ #include <8051reg.h> #include <8051bit.h> #define Data P1.1 #define CLK P1.2 #define CS P1.3 #include "lcddrv.c" #define GPIO2 (*((unsigned char *)0x200)) extern register char lastread; // flag for last byte to be read extern register char cputick; unsigned char flag1,buffer[20],command; char sbuffer[20]; unsigned char sec,min,hour; unsigned int temp,count; unsigned char timer1,timer2; unsigned int record_number,i; char *title = "\n\n8051SBC Based Solar Integrator v1.0"; char *prompt = "\n>>"; struct record { char date; char month; char year; char hour; char min; int insolation; }nvram; struct record rec[720]; int read_ADC(char n) { int k; char i,channel; k=0; clrbit(CS) if(n==0) channel= 0x0d; else channel= 0x0f; // send config nibble dummy read NULL bit after config nibble for(i=0; i<4; i++) { clrbit(CLK) if(channel&8) setbit(Data) else clrbit(Data) setbit(CLK) channel <<=1; } P1 |= 2; // change to input bit clrbit(CLK) // now read 12-bit data for(i=0; i<12; i++) { k<<=1; setbit(CLK) clrbit(CLK) if(P1&2) k |=1; else k &= ~1; } setbit(CS) return k&=0xfff; } send(char a) // Dave's putch() automatically send 0x0d after 0x0a { temp = a; asm{ JNB SCON.1,* CLR SCON.1 MOV A,temp MOV SBUF,A } } output_1Hz() { send_start(); send_byte(0xd0); send_byte(0x07); // pointed to address 7 send_byte(0x90); // enable 1Hz output send_stop(); } readRTC() { send_start(); send_byte(0xD0); send_byte(0x00); send_stop(); send_start(); send_byte(0xD1); lastread = 0; for(i=0; i<6 ; i++) buffer[i] = read_byte(); lastread = 1; buffer[i] = read_byte(); send_stop(); sprintf(sbuffer,"%02x-%02x-%02x %02x:%02x",buffer[4],buffer[5],buffer[6],buffer[2],buffer[1]); goto_xy(0,0); Puts(sbuffer); } initRTC() { send_start(); send_byte(0xD0); send_byte(0x00); send_byte(0x00); send_byte(0x05); send_byte(0x10); send_byte(0x03); send_byte(0x24); send_byte(0x01); send_byte(0x05); send_stop(); } blink_led() { if(++timer1>100) { P1 ^= 0x80; timer1=0; } } read_pyranometer() { if(++timer2>100) { timer2=0; sprintf(buffer,"%04dW/m^2 %d ",read_ADC(0),record_number); goto_xy(0,1); Puts(buffer); if(record_number <10) { save_record(record_number++); printf("\n%d",record_number); } } } get_key_min() { if((GPIO2&0x80) == 0) { printf("\nGPIO2=%02x",GPIO2); } } save_record(int i) { readRTC(); rec[i].date=buffer[4]; rec[i].month=buffer[5]; rec[i].year=buffer[6]; rec[i].hour=buffer[2]; rec[i].min=buffer[1]; rec[i].insolation=read_ADC(0); } print_record(int i) { printf("\n%d %02x-%02x-%02x %02x:%02x %d",i,rec[i].date,rec[i].month,rec[i].year,rec[i].hour,rec[i].min,rec[i].insolation); } char getbyte() // return BCD for DS1307 setting { char s[3]; /* two characters plus terminator */ int i; for (i = 0; i < 2; i++) /* read two characters then return value */ s[i] = putch(getch()); // echo to terminal s[i] = '\0'; return(((s[0]-0x30)<<4)|(s[1]-0x30)); // return BCD // return (_atoi(s)); // return binary } sendprompt() { putstr(prompt); } settime() { if (command == 't') { putstr("\nenter current time HH:MM:SS >"); hour = getbyte(); putch(':'); min = getbyte(); putch(':'); sec = getbyte(); send_start(); send_byte(0xD0); send_byte(0x00); send_byte(sec); send_byte(min); send_byte(hour); send_stop(); sendprompt(); } } prompting() { if (command == 13) { putstr(title); sendprompt(); } } get_record() { if(command == 'g') { for(i=0; i<10; i++) print_record(i); } } void main(void) { InitLcd(); // initRTC(); output_1Hz(); record_number = 0; putstr("\n8051SBC Based Solar Integrator v1.0"); while(1) { while(!cputick) continue; cputick =0; // run following tasks every 10ms command = chkchr(); settime(); prompting(); get_record(); readRTC(); read_pyranometer(); get_key_min(); } }