我写的通过SPI口显示模拟量的小程序,刚学几天的可以看看(刚刚注册,算是我的贡献吧,其实我也刚学几天,呜呜。。。)
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
unsigned char DispBuf0,DispBuf1,DispBuf2;
const unsigned char LED7Code[]={
0xC0, // 0
0xF9, // 1
0xA4, // 2
0xB0, // 3
0x99, // 4
0x92, // 5
0x82, // 6
0xF8, // 7
0x80, // 8
0x90, // 9
0x88, // A
0x83, // B
0xC6, // C
0xA1, // D
0x86, // E
0x8E // F
};
void Spi1Initial() //spi initial
{
SPI1BR=0x00;//8 state: 0 sppr2,sppr1,spper0,0,spr2,spr1,spr0
SPI1CR1=0x50;
SPI1CR2=0x02;
}
void SpiSendData(unsigned char temp)
{
while(!(SPI1SR&0x20));
SPI1DR = temp; /* Send character */
}
void delay(long m) { //delay time
while(m--);
}
unsigned char ad_result;
unsigned char ad_result1;
void TaskAD(void){
ATD0CTL2=0xC0;
ATD0CTL3=0x0C;
ATD0CTL4=0x06; //启动
ATD0CTL5=0x80; //1通道A/D读入 ,右对齐
asm{
movb $80,ATD0STAT0
}
// while(!(ATD0STAT0&0x80))
// {}
//显示A/D结果
ad_result=ATD0DR0L;
ad_result1=ATD0DR0H;
}
void main(void) {
/* put your own code here */
uint m;
TaskAD();
DispBuf1=(ad_result/16);
DispBuf0=(ad_result%16);
DispBuf2=(ad_result1%16);
DDRT=0xff;
Spi1Initial();
PTT|=0x20;
for(m=0;m<500;m++){
SpiSendData(LED7Code[DispBuf0]);
PTT=0x21;
delay(100);
SpiSendData(LED7Code[DispBuf1]);
PTT=0x22;
delay(100);
SpiSendData(LED7Code[DispBuf2]);
PTT=0x24;
delay(100);
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |