SCI應(yīng)用程序
#include 《hidef.h》 /* common defines and macros */
#include “derivative.h” /* derivative-specific definitions */
// 函數(shù)聲明
void SCI0_Init(void);
void SCI0_BR(unsigned long br);
void SCI0_SendByte(char ch);
void Service_WD(void);
void SetBusClock_24MHz(void);
// 全局變量
char SCI_Flag = 0;
char SCI_Rev = 0;
void main(void) {
/* put your own code here */
SetBusClock_24MHz();
SCI0_BR(38400);
SCI0_Init();
EnableInterrupts;
SCI0_SendByte(0x01);
SCI0_SendByte(0x02);
SCI0_SendByte(0x03);
for(;;) {
_FEED_COP(); /* feeds the dog */
if(SCI_Flag==1) {
SCI_Flag = 0;
SCI0_SendByte(SCI_Rev);
}
} /* loop forever */
/* please make sure that you never leave main */
}
void Service_WD(void)
{
CPMUARMCOP = 0x55;
CPMUARMCOP = 0xAA;
}
void SetBusClock_24MHz(void)
{
CPMUOSC_OSCE = 1; /* enable ext osc */
/*
Initialise the system clock from a 16 MHz Crystal,
24 MHz Bus CLK (48 MHz VCO, 48 MHz PLL)
*/
CPMUSYNR = 0x00 | 0x05; /* VCOFRQ[7:6], SYNDIV[5:0] */
CPMUREFDIV = 0x20 | 0x03; /* REFFRQ[7:6], REFDIV[3:0] */
CPMUPOSTDIV = 0x00; /* POSTDIV = 0 FPLL = FVCO */
while(!CPMUFLG_LOCK); /* wait for VCO to stabilize*/
Service_WD();
CPMUCLKS_PLLSEL = 1; /* Switch clk to use PLL */
}
//串口初始化
void SCI0_Init(void)
{
SCI0CR1 = 0x00; /* 8 Data Bits, 1 Start Bit, 1 Stop Bit, No Parity */
SCI0CR2 = 0x2C; /* 使能接收中斷;使能 Tx,Rx */
/* SCIASR1, SCIACR1, SCIACR2, SCISR1, SCISR2, SCIDRH & SCIDRL left at default values */
}
//串口波特率設(shè)置
void SCI0_BR(unsigned long br)
{
uint brPrescaler;
brPrescaler = (uint)(24000000 / (16 * br));
/* Set the Baud Rate */
SCI0BDH = (uchar)((brPrescaler》》8));
SCI0BDL = (uchar)(brPrescaler);
}
//串口發(fā)送字節(jié)
void SCI0_SendByte(char ch)
{
/* check SCI transmit data register is empty */
while(SCI0SR1_TDRE == 0);
SCI0DRL = ch;
}
//串口中斷
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt VectorNumber_Vsci0 SCI0_ISR(void)
{
SCI0CR2_RIE=0;
while(SCI0SR1_RDRF == 0);
SCI_Rev = SCI0DRL;
SCI_Flag = 1;
SCI0CR2_RIE = 1;
}
#pragma CODE_SEG DEFAULT
ADC應(yīng)用程序
#include 《hidef.h》 /* common defines and macros */
#include “derivative.h” /* derivative-specific definitions */
// 函數(shù)聲明
void ADC_Init(void);
uint ADC_GetValue(byte ch);
void Service_WD(void);
void SetBusClock_24MHz(void);
void Delay(void);
// 全局變量
uint AD_Result;
uint AD_Result2;
void main(void) {
/* put your own code here */
SetBusClock_24MHz();
ADC_Init();
EnableInterrupts;
for(;;) {
_FEED_COP(); /* feeds the dog */
AD_Result = ADC_GetValue(7);
AD_Result2 = ADC_GetValue(0);
} /* loop forever */
/* please make sure that you never leave main */
}
// AD初始化
void ADC_Init(void)
{
ATDCTL1 = 0x3F; /* 10-Bit resolution ,discharge before sampling. */
ATDCTL3 = 0x88; /* Right Justified Data, Single conversion sequence */
ATDCTL4 = 0xE1; /* 6 MHz, Notice: 12MHz Max ATD Clock, Fatdlk = FBUS/(2*(PRS+1)) */
/* 26 ATD Clock cycles sample time */
}
// ADC通道采集
uint ADC_GetValue(byte ch)
{
ATDCTL5 = 0x0F & ch; /* Start Continuous Conversions on ch */
while (!ATDSTAT0_SCF); /* wait for conversion sequence to complete */
return ATDDR0;
}
// 看門狗
void Service_WD(void)
{
CPMUARMCOP = 0x55;
CPMUARMCOP = 0xAA;
}
void SetBusClock_24MHz(void)
{
CPMUOSC_OSCE = 1; /* enable ext osc */
/*
Initialise the system clock from a 16 MHz Crystal,
24 MHz Bus CLK (48 MHz VCO, 48 MHz PLL)
*/
CPMUSYNR = 0x00 | 0x05; /* VCOFRQ[7:6], SYNDIV[5:0] */
CPMUREFDIV = 0x20 | 0x03; /* REFFRQ[7:6], REFDIV[3:0] */
CPMUPOSTDIV = 0x00; /* POSTDIV = 0 FPLL = FVCO */
while(!CPMUFLG_LOCK); /* wait for VCO to stabilize*/
Service_WD();
CPMUCLKS_PLLSEL = 1; /* Switch clk to use PLL */
}
void Delay(void)
{
uint dummy_ctr;
for(dummy_ctr=0; dummy_ctr《0x007f;dummy_ctr++)
{
;
}
}
評(píng)論
查看更多