热度 14
2013-4-15 08:43
2148 次阅读|
0 个评论
#include 16F877A.h #device adc=8 #FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc ( 4mhz) #FUSES NOPUT //No Power Up Timer #FUSES NOPROTECT //Code not protected from reading #FUSES NODEBUG //No Debug mode for ICD #FUSES NOBROWNOUT //No brownout reset #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NOCPD //No EE protection #FUSES NOWRT //Program memory not write protected #use delay(clock=20000000) #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) #define OP_WRITE 0xa0 #define OP_READ 0xa1 #define EEPROM_SCL PIN_C0 #define EEPROM_SDA PIN_C1 #bit SCL= 0x07.0 #bit SDA= 0x07.1 #bit SCLIO=0x87.0 #bit SDAIO=0x87.1 void start() { SCLIO=0;delay_us(15); SDA=1;delay_us(15); SCL=1;delay_us(15); SDA=0;delay_us(15); SCL=0;delay_us(15); } void stop() { delay_us(15); SDA=0;delay_us(15); SCL=1;delay_us(15); SDA=1;delay_us(15); } /* 函数功能:读取数据 出口参数:read_data */ unsigned char shin() { unsigned char i, read_data; //设数据脚为输入 SDAIO=1; delay_us(15); for(i=0;i8;i++) { delay_us(15); SCL=1; delay_us(15); read_data=1; if(SDA==1) read_data=read_data+1; delay_us(15); SCL=0; } //设数据脚为输出 SDAIO=0; return(read_data); } /* 函数功能:写入数据 入口参数:write_data */ unsigned char shout(unsigned char write_data) { unsigned char i; unsigned char ack; //设数据脚为输出 SDAIO=0; for(i=0;i8;i++) { if(write_data 0x80) SDA=1; else SDA=0; delay_us(15); SCL=1; delay_us(15); SCL=0; delay_us(15); write_data =1; } delay_us(15); SDA=1; delay_us(15); SCL=1; //设数据脚为输入 SDAIO=1; delay_us(15); ack=SDA; SCL=0; delay_us(15); //设数据脚为输出 SDAIO=0; return ack; } /* 函数功能:向指定地址读数据 入口参数: random_addr */ unsigned char read_random(unsigned char random_addr) { unsigned char read_data; start(); shout(OP_WRITE); //shout(0); shout(random_addr); start(); shout(OP_READ); read_data=shin(); stop(); return(read_data); } /* 函数功能:向指定地址写数据 入口参数: addr , write_data */ void write_byte(unsigned char addr,unsigned char write_data) { start(); shout(OP_WRITE); //shout(0); shout(addr); shout(write_data); stop(); delay_ms(15); } void init_ext_eeprom() { output_float(EEPROM_SCL); output_float(EEPROM_SDA); } char BUFER; #int_rda //RS232 接收到的数据有用, 指定下面的函数是一个中断函数 void serial_isr() { BUFER=getc(); //putc(BUFER); write_byte(0x01,BUFER); //delay_ms(50); putc (read_random(0x01) ); } #INT_RTCC void clock_isr() { SET_RTCC(1500);//20MHz 32分频,初值 0,刚好 1.64 mS 中断一次 //SET_RTCC(500);//20MHz 32分频,初值 0,刚好 166 uS 中断一次 //SET_RTCC(1500);//20MHz 32分频,初值 0,刚好 236 uS 中断一次 //output_toggle(PIN_B0); } void main() { setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_psp(PSP_DISABLED); setup_spi(FALSE); //setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32 |RTCC_8_BIT); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32 ); //设置Timer0 的时钟源为内部时钟源 //每隔256 个脉冲,TMR0 计数 1 次 //RTCC_8_BIT 设置Timer0 为8 位定时器方式; //RTCC_DIV_1 为 288 uS //RTCC_DIV_2 为 544 uS //RTCC_DIV_4 为 1.056 mS //RTCC_DIV_8 为 2.080 mS //RTCC_DIV_16 为 4.10 mS //RTCC_DIV_32 为 8.20 mS SET_RTCC(0); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); enable_interrupts(int_rda); //enable_interrupts(INT_RTCC); enable_interrupts(GLOBAL); // TODO: USER CODE!! //set_tris_c(0); //init_ext_eeprom(); write_byte(0x00,'0');delay_ms(10); write_byte(0x01,'1');delay_ms(10); write_byte(0x02,'2');delay_ms(10); write_byte(0x03,'3');delay_ms(10); write_byte(0x04,'4');delay_ms(10); write_byte(0x05,'5');delay_ms(10); write_byte(0x06,'6');delay_ms(10); write_byte(0x07,'7');delay_ms(10); write_byte(0x08,'8');delay_ms(10); putc (read_random(0x01) );delay_ms(10); putc (read_random(0x02) );delay_ms(10); putc (read_random(0x03) );delay_ms(10); putc (read_random(0x04) );delay_ms(10); putc (read_random(0x05) );delay_ms(10); putc (read_random(0x06) );delay_ms(10); putc (read_random(0x07) );delay_ms(10); putc (read_random(0x08) );delay_ms(10); while(1) { //printf("Freemark!\r\n");delay_ms(1000); //write_byte(0x02,0xaa); } }