关闭

飞思卡尔烟雾传感器main.c

标签: delaymacrossystemincludebytec
1583人阅读 评论(0) 收藏 举报
分类:
#include <hidef.h>      /* common defines and macros */
#include <MC9S12XS128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#include "stdio.h" 
#include "math.h"
#include "LQ12864.h"

#define uchar unsigned char
#define uint  unsigned int

float  data;
uchar Number[140]='0';


void SCIInit(void)
{
      byte tmp;       //定义一个临时变量,用来读寄存器
      SCI0BD =   40000000/16/9600;  //波特率
      SCI0CR1 = 0x00;   //8位数据位,无校验
      SCI0CR2_RE = 1;   
      SCI0CR2_TE = 1;   //允许发送+接收
      SCI0CR2_RIE = 1;  //接收中断
      SCI0CR2_TCIE = 0; //禁止发送完毕中断
      tmp=SCI0SR1;
      SCI0DRH=0x00;
}
unsigned char SciRead()
{
      char ch;
      while (!(SCI0SR1 & 0x20));       //等待接收数据完毕
      SCI0SR1_RDRF = 1;
      ch = SCI0DRL;
      return ch;
 }

 

//SCISend1:串行发送1个字节
void SCiSend(unsigned char o)   
{
      while (!(SCI0SR1&0x80));
      SCI0DRH=0;
      SCI0DRL=o;
}

 

//串口发送N个字节
void SCISend_N(char *a)   
{
    int i=0;
    while(a[i])
    {SCiSend(a[i]);
     i++;
    }
}


//总线时钟初始化
void SetBusCLK_40M(void)
{   
  CLKSEL=0X00;    //disengage PLL to system
  PLLCTL_PLLON=1;   //turn on PLL
  REFDV=1;   
  SYNR=4;  //  总线周期=16Mhz*(SYNR+1)/(REFDV+1)=40MHz 
  while(0==CRGFLG_LOCK);        //  等待  VCO 稳定 
  CLKSEL=0x80;            //  锁相环打开 
  _asm(nop);
  while(!(CRGFLG_LOCK==1));   //when pll is steady ,then use it;
  CLKSEL_PLLSEL =1;          //engage PLL to system; 
}
  
  
//AD初始化
void AD_N_Init(void)
{

      
      ATD0CTL1=0X00;     //8位精度,转换前不放电
      ATD0CTL2=0x40;     // 快速清零, 无等待模式, 禁止外部触发, 中断禁止
      ATD0CTL3=0x88;     // 右对齐无符号,转换序列长度为1, No FIFO, Freeze模式下继续转换
      ATD0CTL4=0x01;     // 为确定值 转换4个时钟, ATDClock=[BusClock*0.5]/[PRS+1]=8MHz; PRS=0,
                         /* 转换时间 = Initial Sample Time + Programmed Sample Time + Resolution Period
                           =2+4+10=16ATDclock=16*1/8=2us */
                          // 要求每隔约2us采集一点 (或更小) 一行采26点
      ATD0CTL5=0x20;     //单通道采样,持续采样,选择通道0   
      ATD0DIEN=0x00;   // 禁止数字输入

     
}


//获取AD值
int AD_N(int i){
 int IOCcnt; 
  char myst[20]=""; 
  
  ATD0CTL2=0x40;
  switch(i){
    case 0:   ATD0CTL5=0x20;break;  
    case 1:   ATD0CTL5=0x21;break; 
    case 2:   ATD0CTL5=0x22;break; 
    case 3:   ATD0CTL5=0x23;break; 
    case 4:   ATD0CTL5=0x24;break; 
    case 5:   ATD0CTL5=0x25;break; 
    case 6:   ATD0CTL5=0x26;break; 
    case 7:   ATD0CTL5=0x27;break; 

    
  }
  
   while(!ATD0STAT0_SCF);
  
  IOCcnt=ATD0DR0L; 
  return   IOCcnt;
}

//液晶显示
void Display() 
{    
    
    
    sprintf(Number,"%.2f",data);
    LCD_P6x8Str(1,1,Number);
}

void delay_1us(void)            //1ms延时函数
{
uint i;
for (i=0;i<3;i++);
}

void delay_nus(uint n)  //N us延时函数
{
 uint j=0;
 for (j=0;j<n;j++)
 delay_1us();
}

 


//IO口初始化
void IOkouInit() 
{SetBusCLK_40M();  
 LCD_Init();
 LCD_CLS();
 AD_N_Init();
 DDRB=0xff;
 PORTB=0xff;
 SCIInit();
}

 

 

void main(void) {
IOkouInit();
while(1) 
        
{       
        data=AD_N(0)/51.0;
        Display();    
        delay_nus(100);
        _FEED_COP(); /* feeds the dog */
 
}
}

 
 #pragma  CODE_SEG NON_BANKED
 void interrupt 20 SCI0_re(void) 
 {
     char ch;
     DisableInterrupts;
    
     ch=SciRead();
     SCISend(ch); 
     
     if(ch=='k') {
  
     
     }  else if(ch=='c') {
       
       }
        
      EnableInterrupts;  
                        
 }  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62017次
    • 积分:623
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:14篇
    • 译文:0篇
    • 评论:18条
    最新评论