我需要用
单片机实现1khz,1V的幅值的正弦信号峰峰值的数码管显示,请问这个程序怎么编写啊!
这是我写的,有什么问题吗!~望高手们帮忙解决。谢谢~!
- #include <pic.h>
- #define UCHAR unsigned char
- #define UINT unsigned int
- __CONFIG(0X3B31);
- UINT aa;
- UINT max,min;
- const UCHAR table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x39,0x5e,0x79,0x71};//数码表
- void DELAY_ms(UINT x)
- {
- UINT a,b;
- for(a=x;a>0;a--)
- for(b=110;b>0;b--);
- }
- void Init()
- {
- TRISA=0X01;
- TRISD=0;
- PORTA=0;
- PORTD=0;
- ADCON0=0X41;
- ADCON1=0X8E;
- DELAY_ms(10);
- }
- UINT AD_GET()
- {
- UINT ADVAL,i=0;
- GO=1;
- ADVAL=ADRESH;
- ADVAL=ADVAL<<8|ADRESL;
- aa=ADVAL;
- GO=0;
- }
- UINT JISUAN()
- {
- UINT i,hh;
- i=0;max=0;min=0;
- while(i<2500)
- {
- aa=AD_GET();
- if(aa>max) {max=aa;}
- if(aa<min) {min=aa;}
- i++;
- }
- hh=max-min;
- return hh;
- }
- void disp(UCHAR num1, UCHAR num2,UCHAR num3,UCHAR num4)
- {
- PORTD=table[num1];
- PORTA=0X20;
- DELAY_ms(2);
- PORTD=table[num2];
- PORTA=0X10;
- DELAY_ms(2);
- PORTD=table[num3];
- PORTA=0X08;
- DELAY_ms(2);
- PORTD=table[num4];
- PORTA=0X04;
- DELAY_ms(2);
- }
- void main()
- {
- UCHAR a1,a2,a3,a4;
- UINT lednum;
- Init();
- while(1)
- {
- lednum=JISUAN();
- a1=lednum/1000;
- a2=lednum%1000/100;
- a3=lednum%100/10;
- a4=lednum%10;
- disp(a1,a2,a3,a4);
- }
- }
复制代码
#define UCHAR unsigned char
#define UINT unsigned int
__CONFIG(0X3B31);
UINT aa;
UINT max,min;
const UCHAR table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x39,0x5e,0x79,0x71};//数码表
void DELAY_ms(UINT x)
{
UINT a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void Init()
{
TRISA=0X01;
TRISD=0;
PORTA=0;
PORTD=0;
ADCON0=0X41;
ADCON1=0X8E; //未开AD采样通道 即ANSEL=?
DELAY_ms(10);
}
UINT AD_GET() //AD采样函数写的有问题 建议再查查手册。
{
UINT ADVAL,i=0;
GO=1;
ADVAL=ADRESH;
ADVAL=ADVAL<<8|ADRESL;
aa=ADVAL;
GO=0;
}
UINT JISUAN()
{
UINT i,hh;
i=0;max=0;min=0;
while(i<2500) //采样这么多次,能知道占用多少机器及周期吗?
{
aa=AD_GET();
if(aa>max) {max=aa;}
if(aa<min) {min=aa;}
i++;
}
hh=max-min;
return hh;
}
void disp(UCHAR num1, UCHAR num2,UCHAR num3,UCHAR num4)
{
PORTD=table[num1];
PORTA=0X20;
DELAY_ms(2);
PORTD=table[num2];
PORTA=0X10;
DELAY_ms(2);
PORTD=table[num3];
PORTA=0X08;
DELAY_ms(2);
PORTD=table[num4];
PORTA=0X04;
DELAY_ms(2);
}
void main()
{
UCHAR a1,a2,a3,a4;
UINT lednum;
Init();
while(1)
{
lednum=JISUAN(); //占用多久时间?
a1=lednum/1000;
a2=lednum%1000/100;
a3=lednum%100/10;
a4=lednum%10;
disp(a1,a2,a3,a4); //建议用定时器刷屏。
}
}
一周热门 更多>