?? uart_samplesession.c
字號:
//============================================================================
// PROJECT = 78K0S/KA1+ Low Pin Count - DemoKit-KA1
// MODULE = uart_samplesession.c
// SHORT DESC. = -
// DEVICE = uPD78F9222
// VERSION = 1.0
// DATE = 22.12.2004
// LAST CHANGE = -
// ===========================================================================
// Description: This sample program simulates a voltage meter with serial
// communication channel. The sample program does a cyclic
// measurement of the input voltage of AD converter channel 0,
// port P20/ANI0, and transfers the measured result via UART6
// to a terminal program running at the host machine. The data
// transfer speed is set to 115200 bps per default. The input
// voltage can be changed by potentiometer R24.
//
// ===========================================================================
#pragma sfr
#pragma DI
#pragma EI
#pragma interrupt INTTM80 isr_INTTM80
#pragma interrupt INTAD isr_INTADC
#pragma section @@CNST OPT AT 80H
const char OPTION=0x9A;
//*************************** chose baudrate *******************************
//#define BAUDRATE 9600
//#define BAUDRATE 38400
#define BAUDRATE 115200
//-----------------------------------------------------------------------------
#define LED1 P2.3 // LED D1
#define LED2 P13.0 // LED D2
#define LED3 P4.5 // LED D3
#define LED4 P12.3 // LED D4
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
unsigned char result_out;
unsigned char result10;
unsigned char result1;
unsigned char result01;
//-----------------------------------------------------------------------------
// Function prototyps
//-----------------------------------------------------------------------------
void drive_LED(unsigned char value);
//-----------------------------------------------------------------------------
// Module: init_CPU
// Function: Initialization of CPU
//-----------------------------------------------------------------------------
void init_CPU (void)
{
// stop watchdog timer
WDTM = 0x70;
//time gererator settings
PCC = 0x00; // set CPU time to fx
PPCC = 0x00;
// HSRCM = 0x00; // high speed ring oscillator operates
LSRCM = 0x01; // low speed ring oscillator stops
OSTS = 0x00; //shortest stabilisation time 2^10/fx
// interrupt setting
IF0 = 0x00;
IF1 = 0x00;
MK0 = 0xFF;
MK1 = 0xFF;
}
//-----------------------------------------------------------------------------
// Module: init_LED
// Function: Initialization of LED ports
//-----------------------------------------------------------------------------
void init_LED (void)
{
PMC2.3=0; // set port mode control of P23 to port mode
PM2.3=0; // set port P23 to output mode -> LED1
PM4.5=0; // set port P45 to output mode -> LED3
PM12.3=0; // set port P123 to output mode -> LED4
LED1=0; // drive LED1
LED2=0; // drive LED2
LED3=0; // drive LED3
LED4=0; // drive LED4
LED1=1; // switch LED1 off
LED2=1; // switch LED2 off
LED3=1; // switch LED3 off
LED4=1; // switch LED4 off
}
//-----------------------------------------------------------------------------
// Module: init_ADC
// Function: Initialization of AD converter
//-----------------------------------------------------------------------------
void init_ADC (void)
{
PMC2.0=1; //set alternate function mode
PM2.0=1; //set port to input mode
ADIF = 0; //clear interrupt request flag
ADMK = 0; //enable ADC interrupt
}
//-----------------------------------------------------------------------------
// Module: init_UART
// Function: Initialization of UART6
//-----------------------------------------------------------------------------
void init_UART(void)
{
PM4.4=1; //input mode for RxD
PM4.3=0; //output mode for TxD
P4.3=1; //set TxD output to high level
POWER6 = 1; //enable internal clock operation
ASIM6 |= 0xE5; //enable transmission
//enable reception
//no parity
//character lenght of data = 8-bits
//number of stop bits = 1
#if BAUDRATE==9600 //initialization for baudrate = 9600
CKSR6 = 0x02;
BRGC6 = 104;
#elif BAUDRATE==38400 //initialization for baudrate = 38400
CKSR6 = 0x00;
BRGC6 = 104;
#elif BAUDRATE==115200 //initialization for baudrate = 115200
CKSR6 = 0x00;
BRGC6 = 35;
#endif
}
//-----------------------------------------------------------------------------
// Module: init_TM80
// Function: Initialization of Timer80
//-----------------------------------------------------------------------------
void init_TM80(unsigned char interval)
{
TCE80 = 0; // stop timer80
TMC80 = 0x06; // set input clock to fxp / 2^10 7.81Khz @ 8MHz
CR80 = interval; // set interval time
TCE80 = 1; // start timer80
TMIF80 = 0; // clear interrupt request flag
TMMK80=0; // enable timer80 interrupt
}
//-----------------------------------------------------------------------------
// Module: UART_SendChar
// Function: Send char via UART6
//-----------------------------------------------------------------------------
void UART_SendChar (unsigned char ucData)
{
TXB6 = ucData; // load UART transmit buffer
while(!STIF6); // wait for transmission finished
STIF6=0;
}
//-----------------------------------------------------------------------------
// Module: UART_GetChar
// Function: Get char from UART6
//-----------------------------------------------------------------------------
unsigned char UART_GetChar(void)
{
unsigned char receive_byte;
while(!SRIF6); // wait for uart receive byte
receive_byte = RXB6; // load UART receive buffer
SRIF6=0;
return (receive_byte);
}
//-----------------------------------------------------------------------------
// Module: UART_SendString
// Function: Send string via UART6
//-----------------------------------------------------------------------------
void UART_SendString (char *ucpStr)
{
unsigned char ucData;
while (1)
{
ucData = (unsigned char)(*ucpStr++);
if(ucData) UART_SendChar (ucData);
else break;
}
}
//-----------------------------------------------------------------------------
// Module: UART_SendResult
// Function: Send measured result via UART6
//-----------------------------------------------------------------------------
void UART_SendResult(void)
{
UART_SendString("Voltage = ");
UART_SendChar(result10+0x30);
UART_SendString(".");
UART_SendChar(result1+0x30);
UART_SendChar(result01+0x30);
UART_SendString(" V\r");
drive_LED(result10);
result_out = 0;
}
//-----------------------------------------------------------------------------
// Module: wait
// Function: generates a wait loop
//-----------------------------------------------------------------------------
__callt void wait(void)
{
char a=0;
while(a!=0xff)
{
a++;
}
}
//-----------------------------------------------------------------------------
// Module: main
// Function: main program
//-----------------------------------------------------------------------------
void main(void)
{
DI(); // global interrupt disable
init_CPU(); // cpu initialization
init_LED(); // led port initialization
init_UART(); // uart60 initialization
UART_SendString("***************************************\n\r");
UART_SendString("* *\n\r");
UART_SendString("* K_Line 78K0S/KA1+ *\n\r");
UART_SendString("* *\n\r");
UART_SendString("* Low Pin Count - Do it! *\n\r");
UART_SendString("* *\n\r");
UART_SendString("***************************************\n\n\r");
UART_SendString("Press a key to start voltage measuring!\n\n\r");
UART_GetChar(); // wait for uart receive byte
UART_SendString("***************************************\n\n\r");
init_ADC(); // adc initialization
init_TM80(60); // initialization of timer80
EI(); // global interrupt enable
while(1)
{
wait();
if (result_out) UART_SendResult();
}
}
//-----------------------------------------------------------------------------
// ISR: isr_INTTM80
// Function: Interrupt service routine of Timer80
//-----------------------------------------------------------------------------
__interrupt void isr_INTTM80(void)
{
ADCE = 1; //enable voltage generator
ADS = 0x00; //AD channel select
ADCS = 1; //start conversion
}
//-----------------------------------------------------------------------------
// ISR: isr_INTADC
// Function: Interrupt service routine of ASD converter
//-----------------------------------------------------------------------------
__interrupt void isr_INTADC(void)
{
unsigned short result;
unsigned char temp;
result = ADCRH; //load AD conversion result
ADCS = 0; //stop AD converter
ADCE = 0; //disable voltage generator
result=result<<1; //convert AD result for
result10 = result/100; //decimal output
result1 = result%100; //
temp = result1; //
result1 = result1/10; //
result01 = temp % 10; //
result_out = 1; //
}
//-----------------------------------------------------------------------------
// Module: drive_LED
// Function: Output of 4-bit value to LED port
//-----------------------------------------------------------------------------
void drive_LED(unsigned char value)
{
LED1 =~(value>>3);
LED2 =~(value>>2);
LED3 =~(value>>1);
LED4 =~(value);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -