MC9S08JM60 12位AD转换的源代码主题部分 :
/********************************************************
* Copyright (c) 2009, LONGQIU S.&T.
* LONGQIU Application Note
* File name : LQadc.c
* Project name: JM60 Evaluation code
* Description : This software is the demo code for JM60 ADC
module
*
* History :
* 2009/01/17 : Initial Development
*
龙丘MC9S08jm60多功能USB开发板V1.0
编写:龙丘
Designed by Chiu Sir
E-mail:chiusir@163.com
软件版本:V1.0
最后更新:2009年01月18日
相关信息参考下列地址:
博客: http://longqiu.21ic.org
淘宝店:http://shop36265907.taobao.com
使用说明:
1.串口9600波特率输出;
2.LCD显示转换后的电压数据;
3.LED指示数据显示状态,闪烁:程序正常,否则死机;
4.12位ADC测试功能,模拟输入用ADP11;
5.总线配置为24MHz;
6.整数转字符数据格式转换方法.
**********************************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include <MC9S08JM60.h>
#include "LQadc.h"
#include "LQ12864.h"
#include "LQprintp.h"
/**********************************************************************************************
* ADC_Init: This function initilizes the ADC module
*
* Parameters: none
*
* Subfunctions: none.
*
*
Return: void
*********************************************************************************************/
void ADC_Init(void)
{
ADCCFG = 0x61;
//busclk/2, Div by 8,ADCK = 1.5MHz
/* 0b0000000 0
* ||||||| |__ bit0,1: ADICLK : input clock
select
* |||||||_|
* ||||||_____ bit2,3: MODE : Conversion
Mode selection
* |||||_|
* ||||______ bit4: ADLSMP: long
sample time configuration
* |||_______ bit5,6 : ADIV: Clock
Divide Select
* ||______|
* |_________ bit7: ADLPC:
Low power configuration
*/
ADCSC2 = 0x00; //
/* 0b00000000
* ||||||||__ bit0:
* |||||||___ bit1:
* ||||||____ bit2:
* |||||_____ bit3:
* ||||______ bit4: ACFGT: Compare function
greater than enable
* |||_______ bit5: ACFE : Compare enable
* ||________ bit6: ADTRG: Conversion trigger
select
* |_________ bit7: ADACT: Convert active
*/
ADCCFG |= 0x04;
//BIT2--3
00:8BIT,01:12BIT,10:102BIT,11:RES
/*Change the channel, to check the releation
between pins and channels */
APCTL1 = 0x00; //others I/O Enabled
APCTL2 = 0x08; //Enable ADP11 ADC
input
}
/****************************************************************************************
* ADC_Cvt : start to conversion of one channel
* input: channel is the channel
number which will do conversion
*
* Return : None
***************************************************************************************/
word ADC_Cvt12B(unsigned char Channel)
{
word AdcResult=0;
if(Channel<12) ADCSC1 =Channel;
else return 0;
while(!ADCSC1_COCO);
AdcResult = ADCRH ;
AdcResult = (AdcResult<<8) |
ADCRL;
return (AdcResult & 0x0FFF);
}
byte ADC_Cvt8B(unsigned char Channel)
{
byte AdcResult=0;
if(Channel<16) ADCSC1 =Channel;
else return 0;
while(!ADCSC1_COCO);
AdcResult = ADCRL;
// AdcResult = ADCR;
return AdcResult;
}
/***************************************************
把0--255的数值转化为1.23V电压格式
****************************************************/
void Word_Cvt_Str(char zifu[],word val)
{
char characters[11]="0123456789";
byte tv=0;
tv=val/1000;
zifu[0] = characters[tv];
zifu[1] = '.';
tv=(val%1000)/100;
zifu[2] = characters[tv];
tv=(val%100)/10;
zifu[3] = characters[tv];
tv=val%10;
zifu[4] = characters[tv];
zifu[5] = 'V';
zifu[6] = '\0';
}
/***************************************************
延时1毫秒函数
****************************************************/
void delayms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2000;jj++); // busclk
24MHz--1ms
}
/***************************************************
总线时钟配置函数
****************************************************/
void MCUInit(void)
{
SOPT1 =
0x13;
// disable COP
MCGC2 = 0x36;
while(!(MCGSC &
0x02)); // wait for the
OSC stable
MCGC1 = 0x98;
while((MCGSC & 0x1C ) != 0x08); // external clock is
selected
MCGC3 = 0x48;
while ((MCGSC & 0x48) != 0x48); // wait for the PLL
is locked
MCGC1 = 0x18;
while((MCGSC & 0x6C) != 0x6C);
}
/***************************************************
main()函数
****************************************************/
void main(void)
{
word res=0;
volatile char txtbuf[16]="1.234V";
MCUInit();
PTFDD =0xff;
PTFD =0x3F;
LCD_Init();
ADC_Init();
uart_init();
LCD_P8x16Str(8,1,"12bit ADC Test");
putstr("==>LongQiu MC9S08JM60 super demo board\n");
putstr("==>12bit ADC test program started...\n");
//EnableInterrupts; /* enable interrupts */
for(;;) {
delayms(500);
res=ADC_Cvt12B(11);
//res=res*328/100*1000/4095;
res=res*4/5;
Word_Cvt_Str(txtbuf,res);
LCD_P8x16Str(16,5,txtbuf);
txtbuf[6]='\n';
txtbuf[7]='\0';
putstr(txtbuf);
PTFD_PTFD7=~ PTFD_PTFD7;
} /* loop forever */
/* please make sure that you never leave main */
}
完整工程如下: