|
200黑幣
整個流程為:mcu通過內(nèi)部中斷PWM方式產(chǎn)生可調(diào)輸出電壓,接到PCF8591的AIN3,返回uchar類型,然后通過mcu顯示到lcd上。
現(xiàn)在問題是:不論我如何改變pwm的占空比,再或者不論P(yáng)CF有沒有輸入都會輸出4.8。之前在考慮是不是i2c讀字節(jié)前需要空讀,但是沒有能夠?qū)懗鰜,求助?br />
有思路的可以留言可以QQ聯(lián)系,謝謝。

只要能解決,500黑幣都給!
main.c
- <div>#include <reg51.h> //51寄存器文件
- #include <intrins.h>
- #include <I2C.H>
- #include"1602.h"
- #define PCF8591 0x90 //PCF8591 地址</div><div>typedef unsigned int uint;//WORD代替unsigned int
- typedef unsigned char uchar;
- unsigned char timer0,timer1,second,second1;
- unsigned int count,count1,mid;
- unsigned int key;
- unsigned char v,ss,aa;
- unsigned long test_ss;
- sbit PWM=P3^7;
- unsigned int i,j;
- unsigned char Data,keyFlag;
- uint a,b,c;
- uchar shi,ge,ms,vt;
- uint a,b,c,o;
- float why;</div><div>void scan_key();
- int T1s();</div><div>uchar dis1[] = {"V1:"};
- uchar dis2[] = {"V2:"};
- uchar dis1t[] = {"T1:"};
- uchar dis2t[] = {"T2:"};
- uchar dist1[] = "00.0s";
- uchar dist2[] = "00.0x";</div><div>void system_Ini()
- {
- TMOD = 0x11;
- //PWM
- TH0 = 0xff;
- TL0 = 0xf7;
- TH1 = 0xff;
- TL1 = 0xf7;
- EA = 1;
- ET0 = 1;
- ET1 = 1;
- TR0 = 1;
- TR1 = 1;
- //IE =0x8A;
- }
- /*******************************************************************
- ADC發(fā)送字節(jié)[命令]數(shù)據(jù)函數(shù)
- *******************************************************************/
- bit ISendByte(unsigned char sla,unsigned char c)
- {
- Start_I2c(); //啟動總線
- SendByte(sla); //發(fā)送器件地址
- if(ack==0)return(0);
- SendByte(c); //發(fā)送數(shù)據(jù)
- if(ack==0)return(0);
- Stop_I2c(); //結(jié)束總線
- return(1);
- }
- /*******************************************************************
- ADC讀字節(jié)數(shù)據(jù)函數(shù)
- *******************************************************************/
- unsigned char IRcvByte(unsigned char sla)
- { unsigned char c = 0;
- Start_I2c(); //啟動總線
- SendByte(sla+1); //發(fā)送器件地址
- if(ack==0)return(0);
- c=RcvByte(); //讀取第1路電壓值,范圍是0-255
- c=0;
- c=RcvByte(); //讀取第1路電壓值,范圍是0-255
- //for(what=0;what<5;what++)//連續(xù)讀5次,取最后一次,以便讀取穩(wěn)定值
-
- Ack_I2c(1); //發(fā)送非就答位
- Stop_I2c(); //結(jié)束總線
- return(c);
- }
- main()
- {
- BYTE i;
- //uchar shi,ge;
- lcd_init(); //初始化LCD
- system_Ini();
- delay(10);
- lcd_pos(0); //設(shè)置顯示位置為第一行的第1個字符
- i = 0;
- while(dis1[i] != '\0')
- { //顯示字符"一號電壓"
- lcd_wdat(dis1[i]);
- i++;
- }
- lcd_pos(8); //設(shè)置顯示位置為第一行的第9個字符
- i = 0;
- while(dis1[i] != '\0')
- { //顯示字符"一號時間"
- lcd_wdat(dis1t[i]);
- i++;
- }
- lcd_pos(0x40); //設(shè)置顯示位置為第二行第1個字符
- i = 0;
- while(dis2[i] != '\0')
- {
- lcd_wdat(dis2[i]); //顯示字符"二號電壓"
- i++;
- }
- lcd_pos(0x48); //設(shè)置顯示位置為第二行第9個字符
- i = 0;
- while(dis2[i] != '\0')
- {
- lcd_wdat(dis2t[i]); //顯示字符"二號時間"
- i++;
- }
- lcd_pos(0x4b); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat(0x30+0);
- lcd_pos(0x4c); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat(0x30+0);
- lcd_pos(0x4d); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat('.');
- lcd_pos(0x4e); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat(0x30+0);
- //while(1);
- lcd_pos(3); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat(0x30+0);
- lcd_pos(4); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat('.');
- lcd_pos(5); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat(0x30+0);
- while(1)
- {
- //if(ms>0) ISendByte(PCF8591,0x43);
- if((count>0)&&(keyFlag == 1))
- {
- ISendByte(PCF8591,0x43);
- Data=IRcvByte(PCF8591);
- ss=Data*48/255;
- a = ss/10 ;
- b = (ss)%10;
- //c = (ss-a*100-b*10)%10;
- lcd_pos(3); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat('0'+a);
- lcd_pos(4); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat('.');
- lcd_pos(5); //設(shè)置顯示位置為第一行的第12個字符
- lcd_wdat(0x30+b);
- //lcd_pos(6); //設(shè)置顯示位置為第一行的第12個字符
- //lcd_wdat(0x30+c);
-
- shi=second/10;
- ge=second%10;
- lcd_pos(0x4b); //設(shè)置顯示位置為第2行的第12個字符
- lcd_wdat(0x30+shi);
- lcd_pos(0x4c); //設(shè)置顯示位置為第2行的第12個字符
- lcd_wdat(0x30+ge);
- lcd_pos(0x4d); //設(shè)置顯示位置為第2行的第12個字符
- lcd_wdat('.');
- lcd_pos(0x4e); //設(shè)置顯示位置為第2行的第12個字符
- lcd_wdat(0x30+ms);
- }
- }
- }
- /*************************************
- [ t1 (0.5ms)中斷] 中斷中做 PWM 輸出
- ------------1000/(0.02ms*250)=200Hz
- *************************************/
- void T0zd(void) interrupt 1 //3 為定時器1的中斷號 1 定時器0的中斷號 0 外部中斷1 2 外部中斷2 4 串口中斷
- {
- scan_key(); //檢測按鍵
- switch(key){
- case 8:
- {
- keyFlag = 1;
- TR0 = 0;
- TH0 = 0xff;
- TL0 = 0xf7;
- TR0 = 1;
- timer1++;
- if(timer1>=100)
- {
- timer1=0;
- ms++;
- vt++;
- }
- if(timer1<=0) PWM=1;
- else PWM=0;
- if(ms == 10)
- {
- ms = 0;
- count++;
- if(count == 10)
- {
- count = 0;
- second++;
- if(second == 60)
- {
- second = 0;
- }
- }}
- else
- {
- }
- }break;
- default:;
- }
- }
- void T1zd(void) interrupt 3 //3 為定時器1的中斷號 1 定時器0的中斷號 0 外部中斷1 2 外部中斷2 4 串口中斷
- {
- }
- void scan_key() //按鍵檢測函數(shù)
- {
- if(P1== 0xfe)
- key= 1;
- if(P1== 0xfd)
- key= 2;
- if(P1== 0xfb)
- key= 3;
- if(P1== 0xf7)
- key= 4;
- if(P1== 0xef)
- key= 5;
- if(P1== 0xdf)
- key= 6;
- if(P1== 0xbf)
- key= 7;
- if(P1== 0x7f)
- key= 8;
- }
- </div>
復(fù)制代碼
I2C.c
|
|