嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
GPIO ,ADC,PWM等实际demo
MC9S08DZ60中文数据手册.pdf
新学习板说明书(MC9S08DZ).pdf
例程源码
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
//===================================
#define LED_DAT PTBD
#define LED_CS0 PTCD_PTCD3
#define LED_CS1 PTCD_PTCD4
#define LED_CS2 PTCD_PTCD5
#define LED_CS3 PTCD_PTCD6
#define LED_CS4 PTCD_PTCD7
#define CS_ON 1
#define CS_OFF 0
const uchar tab[]={
0x3F,/*0*/
0x06,/*1*/
0x5B,/*2*/
0x4F,/*3*/
0x66,/*4*/
0x6D,/*5*/
0x7D,/*6*/
0x07,/*7*/
0x7F,/*8*/
0x6F/*9*/
};//0-9,-,全灭
unsigned int g_adval_0,g_adval_1;
byte ch_s;//通道选择
void Init_Port(void) {
//io初始值都为0
PTBD = 0x00;
PTCD_PTCD2=0;
PTCD_PTCD3=0;
PTCD_PTCD4=0;
PTCD_PTCD5=0;
PTCD_PTCD6=0;
PTCD_PTCD7=0;
//io为输出
PTBDD = 0xFF;
PTCDD_PTCDD2=1;
PTCDD_PTCDD3=1;
PTCDD_PTCDD4=1;
PTCDD_PTCDD5=1;
PTCDD_PTCDD6=1;
PTCDD_PTCDD7=1;
//io为输出
PTBDS = 0xFF;
PTCDS_PTCDS2=1;
PTCDS_PTCDS3=1;
PTCDS_PTCDS4=1;
PTCDS_PTCDS5=1;
PTCDS_PTCDS6=1;
PTCDS_PTCDS7=1;
}
//================================
//函数名:delay
//函数功能:延时函数
//================================
void delay(void)
{
int i ;
for(i=0;i<0x20;i ) __RESET_WATCHDOG();
}
//===================================
//函数名:Init_ADC
//作用:初始化ADC
//================================================================================================================
void Init_ADC(void)
{
ADCCFG=0x94;//低功耗模式,12位精度,ADCK=总线频率
ADCSC2=0x00;//0x00:软件触发,比较功能禁止
APCTL1=0x01;//通道引脚使能:0x01:AD0;0x02:AD1依此类推
//APCTL2=0x04;
APCTL2=0x00;
ch_s=0;//选择0通道
ADCSC1=0x41;//0x40:中断使能,单次转换,选择AD1,AD10并启动了转换.
}
void display(uchar d0 , uchar d1 , uchar d2 , uchar d3 , uchar d4) {
//8个发光二极管
LED_DAT = 0;
LED_CS0 = CS_ON;
LED_CS1 = CS_OFF;
LED_CS2 = CS_OFF;
LED_CS3 = CS_OFF;
LED_CS4 = CS_OFF;
LED_DAT = d0;
delay();
//第四个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_OFF;
LED_CS2 = CS_OFF;
LED_CS3 = CS_OFF;
LED_CS4 = CS_ON;
LED_DAT = d1;
delay();
//第三个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_OFF;
LED_CS2 = CS_OFF;
LED_CS3 = CS_ON;
LED_CS4 = CS_OFF;
LED_DAT = d2;
delay();
//第二个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_OFF;
LED_CS2 = CS_ON;
LED_CS3 = CS_OFF;
LED_CS4 = CS_OFF;
LED_DAT = d3;
delay();
//第一个8
LED_DAT = 0;
LED_CS0 = CS_OFF;
LED_CS1 = CS_ON;
LED_CS2 = CS_OFF;
LED_CS3 = CS_OFF;
LED_CS4 = CS_OFF;
LED_DAT = d4;
delay();
}
void main(void) {
int val;
uint cnt;
Init_Port();
Init_ADC();
EnableInterrupts; /* enable interrupts */
/* include your code here */
for(;;) {
val = g_adval_0;
for(cnt=0 ; cnt<300 ; cnt ){
display(0x55,tab[(val/1000)%10],tab[(val/100)%10],tab[(val/10)%10],tab[val%10]);
//display(0x55,tab[1],tab[2],tab[3],tab[4]);
__RESET_WATCHDOG(); /* feeds the dog */
}
} /* loop forever */
/* please make sure that you never leave main */
}
//=========================================
//==========================================
//ADC转换完成中断服务程序
//==========================================
interrupt VectorNumber_Vadc ADC_ISR(void)
{
switch(ch_s)
{
case 0:
g_adval_0=ADCR;
ADCSC1=0x41;//软件启动下一次转换,下一次转换将转换AD1通道的电压值
break;
case 1:
g_adval_1=ADCR;
ADCSC1=0x40;//软件启动下一次转换,下一次转换将转换AD0通道的电压值
break;
default:
break;
}
asm(nop);//在此设置一个断点,观测ad_h和ad_l的变化
if(ch_s==0)
{
ch_s=1;
}
else
{
ch_s=0;
}
}