查看: 1167|回复: 0
打印 上一主题 下一主题

icc

[复制链接]
跳转到指定楼层
沙发
发表于 2015-5-6 08:33:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <iom8v.h>
#include <macros.h>
#define uchar unsigned char
#define uint  unsigned int
/********************************************************************
                数码管段码定义0123456789
*********************************************************************/
uchar Table[10]={0xc0,0Xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};         //所需的段的位码
uchar wei[4]={0X10,0X20,0X40,0X80};  //位的控制端        (仿真)
//uchar wei[4]={0Xe0,0Xd0,0Xb0,0X70};  //位的控制端        (板子)
uint date;        //定义数据类型
uchar z,x,c,v;        //定义数据类型
/**********************************************************************
                                                                延时函数                                                                          
**********************************************************************/
void DelayMs(uint i)  //0.25ms
{uint j;
for(;i!=0;i--)
  {for(j=8000;j!=0;j--) {;}}
}
/**********************************************************************
                数码管动态扫描
*********************************************************************/
void Pre_Display()
{

/***********************数据转换*****************************/
  z=date/1000;                         //求千位
  x=date%1000/100;                 //求百位
  c=date%100/10;                 //求十位
  v=date%10;                         //求个位

      PORTD=wei[0];
          PORTB=Table[z];
          DelayMs(1);  
            PORTD=wei[1];
      PORTB=Table[x];
          DelayMs(1);  
             PORTD=wei[2];
      PORTB=Table[c];
          DelayMs(1);  
      PORTD=wei[3];
      PORTB=Table[v];
          DelayMs(1);  
  }
/**********************************************************************
                                                                电压值处理函数                                                                          
**********************************************************************/
uint ADC_Convert(void)
{uint temp1,temp2;
temp1=(uint)ADCL;//先读ADCL
temp2=(uint)ADCH;//再读ADCH
temp2=(temp2<<8)+temp1;//求值
temp2=temp2*49/10;//求显示的电压值(0~5.12)V
if(temp2>=5000)temp2=5000;
return(temp2);
}
/**********************************************************************
                      初始化I/O口
**********************************************************************/
void Init_IO(void)             //初始化I/O口
{
DDRC=0x00;                    //设置C口为不带上拉输入;
PORTC=0x00;
DDRB=0xff;                    //设置B口为推挽1输出;
PORTB=0xff;
DDRD=0xFF;                    //设置D口为推挽1输出;
PORTD=0xFF;
}
/********************************************************************
                      串口初始化子程序
*********************************************************************/
void Uart_Init(void)
{
    UCSRB  =  0x00;     //disable while setting baud rate
    UCSRA  =  0x00;     //Bit1为1则倍速发送
    UCSRC  =  0x86;  
    UBRRL  =  0x33;     //波特率:9600 Bps
    UBRRH  =  0x00;     //误差率:0.156%
    UCSRB  =  0x18;  
}
/********************************************************************
                     串口发送字节子程序
*********************************************************************/
void Putchar(unsigned char c)
{
   while (!(UCSRA&(1<<UDRE)));
   UDR=c;
}
/**********************************************************************
                                                                主函数                                                                          
**********************************************************************/
void main(void)
{uint i;
uint B=50;
uint TXD;
Init_IO();
Uart_Init();
ADMUX=0x00;

                   //选择第一通道;
/**********************转换速率应在50~200K****************************/
ADCSR=0xe6;                   //125K转换速率,自由转换模式;启动AD转换器;
DelayMs(1);                //延时待系统稳定;
while(1)
{

B--;
i=ADC_Convert();   //显示电压值(0~1024)

TXD=i/19;          //串口发送数据(0~255)
if(TXD>=255)TXD=255;
if(TXD<=0)TXD=0;
Pre_Display();
if(B==0)
{
B=50;
date=i;
Putchar(TXD);
}  
  }
}
/**********************************************************************
                                                                程序结束                                                                          
**********************************************************************/




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?加入因仑

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入因仑

本版积分规则

快速回复 返回顶部 返回列表