📄 main.c
字号:
/*
Travis Lytle
ECET 309 serial communication with interrupts
This takes the input from SCI0 and flashes an led when any input is recieved.
When ctrl-x is enter the input is transmitted back out on the SCI0
*/
#include <hidef.h> /* common defines and macros */
#include <mc9s12db128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12db128a"
void initSCI0(void);
void initOC0(void);
void delay(int);
unsigned char textinput[40];
unsigned int cnt;
char x =0;
#pragma CODE_SEG NON_BANKED
void interrupt 20 isr_SCI0(void) //checks to see if in send or receive mode
{
if((SCI0SR1 & 0x20) == 0x20){ //check receive flag (RDRF)
char x;
x = SCI0SR1;
textinput[cnt] = SCI0DRL;
cnt++;
PORTA = 0xFF;
delay(2);
PORTA = 0x00;
}
if((SCI0SR1 & 0x80) == 0x80){ //check transmit flag (TDRE)
cnt =0;
while(cnt <41) {
if ((SCI0SR1 & 0x40) == 0x40){
SCI0DRL = textinput[cnt];
cnt++;
}
}
//clear flags
SCI0CR2 = 0x24;
x = SCI0SR1; //status reg clears RDRF flag
x = SCI0DRL; //data reg
SCI0CR2 = 0x24;
//reset counter and clear array
cnt =0;
for(cnt;cnt<41;cnt++){
textinput[cnt]=0;
}
cnt =0;
}
}
#pragma CODE_SEG DEFAULT
void main(void) {
initOC0();
initSCI0();
DDRA = 0xFF;
x = SCI0SR1;
SCI0DRL = x;
EnableInterrupts;
while(1)
{
if(cnt != 0)
if(textinput[(cnt-1)] == 0x18) // check for ctrl-x if so go into transmit mode
SCI0CR2 = 0x88;
}
}
//Serial Comm Interface initalization
void initSCI0() {
SCI0BDH = 0x00; //Baud Rate H Byte
SCI0BDL = 26; //Baud Rate L Byte 9600baud
SCI0CR1 = 0x00;
SCI0CR2 = 0x24; // 0x24 recieve only 0x88 transmit only
x = SCI0SR1; //status reg clears RDRF flag
x = SCI0DRL; //data reg
}
//initialization of the output compare timer
void initOC0()
{
TIOS = 1;
TSCR2 = 4;
TSCR1 = 0x80;
}
//delay code converted from assembly
void delay(int cnt) //accepts value for the amount of delay wanted
{
TCNT = TC0;
while(cnt > 0) //while passed value cnt is greater than zero do the following
{
TFLG1 = TFLG1 | 1;
TC0 = TC0 + 1500;
while((TFLG1 & 1)==0){;} //while TFLG1 logically bit anded with 1 equal 0, wait here
cnt--; //decrement counter
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -