下面是NSS08Kit-R1开发板的例程,用来演示Freescale S08单片机的ADC模块,这个程序是基于MC9S08AC16的,但是应该同样适用于其他型号的S08单片机(可能需要稍微的修改)。这个程序每个1秒执行一次AD转换,并将转换结果通过串口发给PC,可以在PC上通过串口调试助手等软件观察结果。这里是使用了AC16的两路ADC,轮流转换。
/*****************************
@ Nicrosystem Freescale S08 DevKit(NSS08Kit-R1)
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-04-06
******************************/
/*********************
@说明:
本程序测试单片机片上ADC功能,它启动两个通道的AD转换,并将转换结果
通过串口发送到PC上,可以在PC端使用串口调试助手等工具进行观察
*********************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "delay.h"
#include "beep.h"
#include <stdio.h>
#define BUS_CLK 4000000L
typedef enum
{
DONT_COMPLETE=0,
CHANNEL0=1,
CHANNEL1=2
}ADC_RESULT;
ADC_RESULT g_adcComplete;
unsigned int adc_result;
//以查询方式发送一个字节
void uart2_send_byte_poll(unsigned char c) {
/*SCI2C2_TIE =0;
SCI2C2_TCIE=0;
SCI2C2_RIE=0;
SCI2C2_ILIE=0;*/
SCI2D=c;
while(SCI2S1_TDRE==0);
}
unsigned char uart2_recv_byte_poll() {
/* SCI2C2_TIE =0;
SCI2C2_TCIE=0;
SCI2C2_RIE=0;
SCI2C2_ILIE=0; */
while(SCI2S1_RDRF==0);
return SCI2D;
}
//以查询方式发送一个字符串
void uart2_send_string_poll(unsigned char *str) {
while(*str!='\0'){
uart2_send_byte_poll(*str++);
}
}
//初始化sci2
//设置sci2为8bit,一个停止位,无校验模式
//设置sci2发送,接收使能,中断全部禁止
//设置波特率为9600
void init_sci2()
{
unsigned int BaudRate;
BaudRate="BUS"_CLK/9600/16;
SCI2BDH=(unsigned char)(BaudRate>>8);
SCI2BDL=(unsigned char)(BaudRate&0x00ff);
SCI2C1=0x00;
SCI2C2=0x0c;
SCI2C3=0x00;
}
//初始化ADC
//设置ADC通道0,10位分辨率,软件触发方式,单步转换
void init_adc_channel()
{
ADC1SC2=0x00;//compare function disable,software trigger
ADC1CFG=0x08;//using bus clock(4MHZ),10bit resoultion,short sample time
ADC1SC1=0x40;
}
void select_adc_channel(unsigned char channel)
{
ADC1SC1_ADCH =channel;
}
//设置系统时钟模式,为FEE,8MHZ的系统主频,BUS CLK为4MHZ
void init_system_clock()
{
ICGC1=0xf8;
ICGC2=0x89;
while(ICGS1_LOCK==0);
}
interrupt VectorNumber_Vadc1 void adc1_isr()
{
adc_result=(unsigned int)(ADC1RH)<<8;
adc_result+=ADC1RL;
if(g_adcComplete>=CHANNEL1)
g_adcComplete=CHANNEL0;
else
g_adcComplete++;
}
void main(void) {
unsigned char uart2buf[50];
EnableInterrupts; /* enable interrupts */
/* include your code here */
SOPT_COPE=0;//disable cop watchdog
init_system_clock();
g_adcComplete=DONT_COMPLETE;
init_sci2();
uart2_send_string_poll((unsigned char*)("start adc channel\n")) ;
init_adc_channel();
for(;;) {
// __RESET_WATCHDOG(); /* feeds the dog */
if(g_adcComplete!=DONT_COMPLETE)
{
sprintf(uart2buf,"ADC Channel %d result:%d\n",g_adcComplete-1,adc_result);
uart2_send_string_poll(uart2buf);
if(g_adcComplete==CHANNEL0)
select_adc_channel(1);
else
select_adc_channel(0);
delay_1s(1);
}
} /* loop forever */
/* please make sure that you never leave main */
}
文章评论(0条评论)
登录后参与讨论