📄 main.c
字号:
//程序的调试可以用"串口调试助手V2.1"辅助完成
#include "p18cxxx.h"
#pragma config OSC = XT, OSCS = OFF
#pragma config PWRT = ON
#pragma config WDTPS =32768
#pragma config CCP2MX = ON
#pragma config MCLRE = OFF
#pragma config BOR = ON //RC2 OK
#pragma config BORV = 27
#pragma config WDT = ON
#pragma config LVP = OFF
//#include "p18f6585.h"
#include <timers.h>
#define on 1
extern void Cmd_dispose( void );
extern void ctrl_8816(void);
void Tx_Sing_State(void);
void sound_check( void ); //信号检测
void Tx_8816_State ( void ); //驱动232返回8816设置消息
unsigned char TX8816; //信号,请求232返回8816设置消息
void InterruptHandlerHigh(void); //中断服务程序
void timer_isr (void); //中断服务程序
unsigned char RC_s[10]={0,0,0,0,0,0,0,0,0,0};//字符数组
unsigned char SignalList[3]={0,0,0};
unsigned char Err;
static unsigned char RC_Count;//接受记数
unsigned char byte_Count;//字符数组的长度数
static unsigned char TX_Count;//发送记数
static unsigned char RC_Over;
static unsigned int Led_count;
/*
* For PIC18xxxx devices, the low interrupt vector is found at 000000018h.
* Change the default code section to the absolute code section named
* low_vector located at address 0x18.
*/
//底优先级中断向量
#pragma code low_vector=0x18
void low_interrupt (void)
{
/*
* Inline assembly that will jump to the ISR.
*/
_asm GOTO timer_isr _endasm //跳到低优先级中断程序
}
/*
* Returns the compiler to the default code section.
*/
#pragma code
/*
* Specifies the function timer_isr as a low-priority interrupt service
* routine. This is required in order for the compiler to generate a
* RETFIE instruction instead of a RETURN instruction for the timer_isr
* function.
*/
#pragma interruptlow timer_isr
/*
* Define the timer_isr function. Notice that it does not take any
* parameters, and does not return anything (as required by ISRs).
*/
void
timer_isr (void)
{
/*
* Clears the TMR0 interrupt flag to stop the program from processing the
* same interrupt multiple times.
*/
INTCONbits.TMR0IF = 0; //Clear Timer0 overflow flag
T0CONbits.TMR0ON = 0; //8ms超时,停止TMR0,停止限时监控
//下面写用户程序
RC_Over = 1; //产生超时事件
byte_Count = RC_Count; //保存接受字符数
RC_Count = 0; //准备再次接受串行口信号
TX_Count=0; //准备发送记数
PORTD=0;
}
//高优先级中断向量
#pragma code InterruptVectorHigh=0x08
void InterruptVectorHigh (void)
{
_asm
goto InterruptHandlerHigh //跳到高优先级中断程序
_endasm
}
//高优先级中断服务程序
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh ()
{
while(PIR1bits.RCIF==1) //若接收中断标志不为1,返回
{
PIR1bits.RCIF=0;
RC_s[RC_Count++]=RCREG; //将接收到的数据放入RC_s[]
//TXREG=RCREG;
TMR0L = 0; // Reset Timer0 to 0x0000
T0CONbits.TMR0ON = 1; // 起动限时监控
}
if(TX_Count<byte_Count) //需要发送
{
if(PIR1bits.TXIF==1) //若缓冲空
{
TXREG=RC_s[TX_Count++]; //发送一个字节,发送计数加一
}
if(TX_Count==byte_Count) //不需要发送
PIE1bits.TXIE=0; //发送中断不使能
}
}
//初始化程序
void
OpenUart(void)
{
SPBRG=0x19; //选择传输波特率为9600bps
TXSTA=0X04; //选择异步高速方式传输8位数据
RCSTA=0X80; //允许异步串行口工作
TRISC=0XC0; //将RC7,RC6设置为输入
BAUDCONbits.BRG16=0; //高速波特率
TXSTAbits.TXEN=1; //发送允许
RCSTAbits.CREN=1; //接受允许
PIE1bits.RCIE=1; //接收中断使能
IPR1bits.RCIP=1; //接受高中断优先级中断使能
IPR1bits.TXIP=1; //发送高中断优先级中断使能
RC_Count=0;
RC_Over=0;
TX_Count=0;
}
void OpenMyTimer0(unsigned char config)
{
T0CON = (0x7f & config); // Configure timer, but don't start it yet
INTCONbits.T0IF = 0; // Clear Timer0 overflow flag
INTCONbits.T0IE = 1; // Enable Timer0 overflow interrupt
INTCON2bits.TMR0IP=0; //TMR0低中断优先级中断使能
TMR0H = 0x70; //定时器高八位
TMR0L = 0; // Reset Timer0 to 0x7000
}
//主程序
void
main(void)
{
RCONbits.IPEN=1; //使能中断优先级
OpenUart(); //建立串口通信条件
OpenMyTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT);//建立串口通信限时条件
INTCON|=0XC0; //总中断和外围中断允许
TRISE=0X7F; //声音信号检测位输入使能S0~S6
TRISD=0X7F; //声音信号检测位输入使能S20~S26
TRISB=0X7F; //声音信号检测位输入使能S30~S36
TX8816=0;
byte_Count=0;
Led_count=0X1FFF;
//循环执行下面任务
for(;;){
_asm CLRWDT _endasm
if(Led_count--==0){ //定时一秒左右
Led_count=0X1FFF;
if(TX8816==0);
// Tx_Sing_State(); //下位机定时发送声音信号消息
else
{ Tx_8816_State();TX8816=0;} //下位机发送8816消息
PORTDbits.RD7=~PORTDbits.RD7; //LED FLASH
}
if(RC_Over==1){ //发生232接受结束事件
RC_Over=0; //清除232接受结束事件标志
Cmd_dispose(); //命令处理
Tx_Sing_State(); //下位机定时发送声音信号消息
}
//sound_check(); //外部声音信号检查
}
}
void sound_check()
{
//RC_s[0]=2;RC_s[1]=2;RC_s[4]=1;byte_Count=5; //状态回复指令<有语音>:02 02 bb bb 01
//RC_s[0]=2;RC_s[1]=3;RC_s[4]=1;byte_Count=5; //状态回复指令<无语音>:02 03 bb bb 01
if((RC_s[0]==2)&&(RC_s[1]==4)&&(RC_s[4]==1)){ //查询错误指令:02 04 bb bb 01
goto end_check;}
RC_s[0]=2;RC_s[4]=1;byte_Count=5; //状态回复指令
switch(RC_s[2]){
case 1: if(PORTEbits.RE0==on)RC_s[1]=2;else RC_s[1]=3;break;
case 2: if(PORTEbits.RE1==on)RC_s[1]=2;else RC_s[1]=3;break;
case 3: if(PORTEbits.RE2==on)RC_s[1]=2;else RC_s[1]=3;break;
case 4: if(PORTEbits.RE3==on)RC_s[1]=2;else RC_s[1]=3;break;
case 5: if(PORTEbits.RE4==on)RC_s[1]=2;else RC_s[1]=3;break;
case 6: if(PORTEbits.RE5==on)RC_s[1]=2;else RC_s[1]=3;break;
case 7: if(PORTEbits.RE6==on)RC_s[1]=2;else RC_s[1]=3;break;
case 8: if(PORTDbits.RD0==on)RC_s[1]=2;else RC_s[1]=3;break;
case 9: if(PORTDbits.RD1==on)RC_s[1]=2;else RC_s[1]=3;break;
case 10: if(PORTDbits.RD2==on)RC_s[1]=2;else RC_s[1]=3;break;
case 11: if(PORTDbits.RD3==on)RC_s[1]=2;else RC_s[1]=3;break;
case 12: if(PORTDbits.RD4==on)RC_s[1]=2;else RC_s[1]=3;break;
case 13: if(PORTDbits.RD5==on)RC_s[1]=2;else RC_s[1]=3;break;
case 14: if(PORTDbits.RD6==on)RC_s[1]=2;else RC_s[1]=3;break;
case 15: if(PORTBbits.RB0==on)RC_s[1]=2;else RC_s[1]=3;break;
case 16: if(PORTBbits.RB1==on)RC_s[1]=2;else RC_s[1]=3;break;
case 17: if(PORTBbits.RB2==on)RC_s[1]=2;else RC_s[1]=3;break;
case 18: if(PORTBbits.RB3==on)RC_s[1]=2;else RC_s[1]=3;break;
case 19: if(PORTBbits.RB4==on)RC_s[1]=2;else RC_s[1]=3;break;
case 20: if(PORTBbits.RB5==on)RC_s[1]=2;else RC_s[1]=3;break;
case 21: if(PORTBbits.RB6==on)RC_s[1]=2;else RC_s[1]=3;break;
default: RC_s[1]==4; break;
}
end_check:;
}
void Tx_Sing_State (void)
{
RC_s[5]=SignalList[0];
RC_s[6]=SignalList[1];
RC_s[7]=SignalList[2];
byte_Count+=3;
PIE1bits.TXIE=1; //发送中断使能,232发送
}
void Tx_8816_State()
{
PIE1bits.TXIE=1; //发送中断使能,232发送
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -