📄 d matrix.c
字号:
//程序的调试可以用"串口调试助手V2.1"辅助完成2008.2.10.20.24
#include "p18cxxx.h"
//#include "p18f6585.h"
#include <timers.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
#define on 0
#define off 1
#define RESET_8816 PORTGbits.RG0
#define STROBE_8816 PORTGbits.RG1
#define CS11_8816 PORTGbits.RG2
#define CS21_8816 PORTGbits.RG3
#define CS31_8816 PORTGbits.RG4
#define S10 PORTEbits.RE0
void InterruptHandlerHigh(void); //中断服务程序
void timer_isr (void); //中断服务程序
void Off_8816(void);
void On_8816(void);
unsigned char RC_s[10];//字符数组
unsigned char SignalList[3]={0,0,0};
unsigned char Err;
unsigned char Ch_See;
unsigned char Ch_lock;
static unsigned char RC_Count;//232接受字节记计数
unsigned char byte_Count;//字符数组的长度数
static unsigned char TX_Count;//232发送字节记计数
static unsigned char RC_Over;//232接受超时
static unsigned int delay_count;//定时计数
unsigned char t;//定时计数2
/*
* 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; //准备再次接受串行口信号
}
//高优先级中断向量
#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[]
TMR0L = 0; // Reset Timer0 to 0x0000
T0CONbits.TMR0ON = 1; // 起动限时监控
}
if((TX_Count<byte_Count)&&(PIE1bits.TXIE)) //需要发送
{
if(PIR1bits.TXIF==1){ //若缓冲空
TXREG=RC_s[TX_Count++]; //发送一个字节,发送计数加一
}
if(TX_Count==byte_Count){ //不需要发送
PIE1bits.TXIE=0; //发送中断不使能
TX_Count=0; //准备再次发送记数
byte_Count=0;
}
}
}
//初始化程序
void
OpenUart(void)
{
SPBRG=0x19; //选择传输波特率为9600bps
TXSTA=0X04; //选择异步高速方式传输8位数据
RCSTA=0X80; //允许异步串行口工作
TRISC=0X80; //将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 test(void){
static unsigned char t;
RC_s[0]=0;
RC_s[1]=0;
RC_s[2]=t++;RC_s[3]=0;RC_s[4]=0;
byte_Count=5;
}
void send(void){
PIE1bits.TXIE=1; //发送中断使能,232发送
while(PIE1bits.TXIE==1)
{_asm CLRWDT _endasm} //忙等待
}
//主程序
void
main(void)
{
_asm CLRWDT _endasm
TRISE=0XFF; //声音信号检测位输入使能S0~S6
TRISD=0X7F; //声音信号检测位输入使能S20~S26
TRISB=0XFF; //声音信号检测位输入使能S30~S36
CMCON=7; //RF3:6 IS I/O
ADCON1=0X7F; //AN0:15 IS I/O
TRISF=0X00; //F0-F7---AX0-AX3,AY0-AY2,DATE
PORTF=0;
TRISG=0X00; //RG0:RESAT,RG1:STROBE,RG2:CS11,RG3:CS21,RG4:CS31,
delay_count=0XFFFF; //软件定时器初始值
Ch_See=0; //监控外部节点信号初始值
RESET_8816=1; //8816复位,切断8816内部连接
CS11_8816=0; //8816复位CS,不选中
CS21_8816=0; //8816复位CS,不选中
CS31_8816=0; //8816复位CS,不选中
RESET_8816=0; //8816复位结束
RCONbits.IPEN=1; //使能中断优先级
OpenUart(); //建立串口通信条件
OpenMyTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT);//建立串口通信限时条件
INTCON|=0XC0; //总中断和外围中断允许
//循环执行任务
for(;;){ //test();
_asm CLRWDT _endasm
if(S10==0){//如1号口有信号(0电平)
Off_8816();//所有PTT输入与输出断开。
delay_count=0xffff;//定时复位
}
if(delay_count!=0){//定时事件处理
delay_count--;
t=50;
while(t--);
_asm CLRWDT _endasm
}
else{//自己连接
On_8816();//所有PTT输入与输出连接。
}
}
//循环执行任务
}
void
Off_8816(void){
RESET_8816=1; //8816复位,切断8816内部连接
_asm CLRWDT _endasm
RESET_8816=0; //8816复位结束
}
void
On_8816(void){ //音源出(X1~X21)接到(Y1~Y21)
unsigned char Decoder,i;
CS11_8816=1; //全部选片
CS21_8816=1;
CS31_8816=1;
for(i=0;i<7;i++){
Decoder=i; //加入(X?)
if(i==6)Decoder=8;
Decoder<<=4;
Decoder+=i;
Decoder|= 0X80; //接到BUF(Y7)和D==1选择连接
PORTF=Decoder; //数据输出
STROBE_8816=1;
_asm NOP _endasm
STROBE_8816=0;//Set STROBE falling //使能
}
CS11_8816=0; //全部结束选片
CS21_8816=0;
CS31_8816=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -