打印
[疑难问答]

AD之后的数据怎么到UART0的?

[复制链接]
1877|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
qfengbj|  楼主 | 2010-6-18 12:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这程序的意思是不是把AD之后的温度通过UART0的TX0(P0.0)发送到LCD上?

AD之后的数据怎么到UART0的?
本人刚开始学单片机,不知道哪位愿意解释一下整个程序的过程(从AD到LCD的输出)?
沙发
qfengbj|  楼主 | 2010-6-18 12:35 | 只看该作者
#include <c8051f020.h>               
#include <stdio.h>
#include <INTRINS.H>
sfr16 DP       = 0x82;
sfr16 TMR3RL   = 0x92;
sfr16 TMR3     = 0x94;
sfr16 ADC0     = 0xbe;
sfr16 ADC0GT   = 0xc4;
sfr16 ADC0LT   = 0xc6;
sfr16 RCAP2    = 0xca;
sfr16 T2       = 0xcc;
sfr16 RCAP4    = 0xe4;
sfr16 T4       = 0xf4;
sfr16 DAC0     = 0xd2;
sfr16 DAC1     = 0xd5;

#define BAUDRATE     115200
#define SYSCLK       22118400
#define SAMPLE_RATE  50000
#define INT_DEC      256
#define        AMX0SL_AIN   8

void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
void LCD_Init(void);

unsigned char xdata NCDdata[6]={0x30,0x30,0x30,0x2e,0x30,0x30};
long result;
unsigned char *lcdpoint;        
unsigned char lcd_data_count;   

void main (void) {
   long temperature,x;
   int temp_int, temp_frac;
   char data1;

   WDTCN = 0xde;  
   WDTCN = 0xad;

   SYSCLK_Init ();
   PORT_Init ();
   UART0_Init ();
   Timer3_Init (SYSCLK/SAMPLE_RATE);


   ADC0_Init ();

        AD0EN = 1;

    EA = 1;  
        while(result==0);
        while (1) {
      EA = 0;
      temperature = result;
      EA = 1;

      temperature = temperature - 42380;
      temperature = (temperature * 100L) / 156;
      temp_int = temperature / 100;
      temp_frac = temperature - (temp_int * 100);
          printf ("Temperature is %+02d.%02d\n", temp_int, temp_frac);/

          NCDdata[0]=temp_int/100+0x30;NCDdata[1]=(temp_int%100)/10+0x30;NCDdata[2]=(temp_int%100)%10+0x30;
          NCDdata[4]=temp_frac/10+0x30;NCDdata[5]=temp_frac%10+0x30;


          LCD_Init();
          P2  = 0xA0;
          lcdpoint=&NCDdata;
          for(lcd_data_count=6;lcd_data_count>0;lcd_data_count--)
          {
                          data1=*lcdpoint;
                    P3 = data1;
                        P2 = 0X20;
                P2 = 0XA0;
                lcdpoint++;
                        for(x=0;x<0x5000;x++);
          }
          for(data1=0;data1<0x6;data1++)
          {
                        for(x=0;x<0xffff;x++)
                          {_nop_();}
       }
        }
}


void SYSCLK_Init (void)
{
   int i;

   OSCXCN = 0x67;
   for (i=0; i < 256; i++) ;

   while (!(OSCXCN & 0x80)) ;

   OSCICN = 0x88;
}


void PORT_Init (void)
{
   XBR0    = 0x04;
   XBR1    = 0x00;
   XBR2    = 0x40;
   P0MDOUT |= 0x01;
   P2MDOUT  = 0xe0;
   P3MDOUT  = 0xff;
}

void UART0_Init (void)
{
   SCON0   = 0x50;
   TMOD    = 0x20;
   TH1    = -(SYSCLK/BAUDRATE/16);     // set Timer1 reload value for baudrate
   TR1    = 1;                         // start Timer1
   CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
   PCON  |= 0x80;                      // SMOD00 = 1
   TI0    = 1;                         // Indicate TX0 ready
}

//-----------------------------------------------------------------------------
// ADC0配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
   ADC0CN = 0x05;                      // ADC0 T3定时采样,左对齐
   REF0CN = 0x07;                      // 启用内部基准源
   AMX0SL = AMX0SL_AIN;                // 选择采样输入源
   ADC0CF = (SYSCLK/2500000) << 3;     // ADC conversion clock = 2.5MHz
   ADC0CF |= 0x01;                     // PGA gain = 2

   EIE2 |= 0x02;                       // 启用 ADC 中断
}

//-----------------------------------------------------------------------------
// Timer3配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void Timer3_Init (int counts)
{
   TMR3CN = 0x02;
   TMR3RL  = -counts;
   TMR3    = 0xffff;
   EIE2   &= ~0x01;
   TMR3CN |= 0x04;
}

//-----------------------------------------------------------------------------
// ADC0采样中断
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 15
{
   static unsigned int_dec=INT_DEC;   
   static long accumulator=0L;                     

   AD0INT = 0;                                                   // 清 ADC 中断标志位


        accumulator += ADC0;               // 累加ADC采样数据
   int_dec--;                          // 指针减1

   if (int_dec == 0) {                 // 累加完了吗?
      int_dec = INT_DEC;               // 指针复位
      result = accumulator >> 8;
      accumulator = 0L;                // 累加和变量清0
   }
}
//LCD初始化
void LCD_Init(void)
{unsigned long x;
   P2 = 0X80;
   for(x=0;x<1000;x++);
   //P7 = 0x30;                                /*一行显示*/
   P3 = 0x38;                                /*两行显示*/
   P2 = 0X00;//0x08;
   P2 = 0X80;//0x09;
   for(x=0;x<1000;x++);
   P3 = 0x0e;
   P2 = 0x00;
   P2 = 0x80;
   for(x=0;x<1000;x++);
   P3=  0x06;
   P2 = 0x00;
   P2 = 0x80;
   for(x=0;x<5000;x++);
   P3 = 0x01;
   P2 = 0x00;
   P2 = 0x80;
   for(x=0;x<5000;x++);
}

使用特权

评论回复
板凳
qfengbj|  楼主 | 2010-6-19 22:15 | 只看该作者
AD之后的数据怎么到UART0的?
程序是长了点:lol

使用特权

评论回复
地板
guita| | 2010-6-19 22:19 | 只看该作者
好长的程序啊,先慢慢看一下

使用特权

评论回复
5
qfengbj|  楼主 | 2010-6-22 23:17 | 只看该作者
程序是长了点,不知道能不能碰到高手

使用特权

评论回复
6
lpydidi| | 2010-6-30 14:52 | 只看该作者
通过串口中断发送啊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

158

帖子

1

粉丝