|
GYJ-0320 某一個(gè)寶描述_01.jpg (648.54 KB, 下載次數(shù): 16)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_02.jpg (286.86 KB, 下載次數(shù): 17)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_09.jpg (458.6 KB, 下載次數(shù): 17)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_10.jpg (224.04 KB, 下載次數(shù): 16)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_11.jpg (752.09 KB, 下載次數(shù): 16)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_12.jpg (484.61 KB, 下載次數(shù): 21)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_13.jpg (266.59 KB, 下載次數(shù): 19)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_14.jpg (443.26 KB, 下載次數(shù): 14)
下載附件
2023-7-1 18:52 上傳
GYJ-0320 某一個(gè)寶描述_16.jpg (305.12 KB, 下載次數(shù): 21)
下載附件
2023-7-1 18:52 上傳
【聲明】此程序僅用于學(xué)習(xí)與參考!
*********************************************************************/
/************* 功能說明 **************
本例程基于STC32G為主控芯片的實(shí)驗(yàn)箱進(jìn)行編寫測(cè)試。
使用Keil C251編譯器,Memory Model推薦設(shè)置XSmall模式,默認(rèn)定義變量在edata,單時(shí)鐘存取訪問速度快。
edata建議保留1K給堆棧使用,空間不夠時(shí)可將大數(shù)組、不常用變量加xdata關(guān)鍵字定義到xdata空間。
串口2全雙工中斷方式收發(fā)通訊程序。
通過PC向MCU發(fā)送數(shù)據(jù), MCU收到后通過串口2把收到的數(shù)據(jù)原樣返回.
用定時(shí)器做波特率發(fā)生器,建議使用1T模式(除非低波特率用12T),并選擇可被波特率整除的時(shí)鐘頻率,以提高精度。
下載時(shí), 選擇時(shí)鐘 22.1184MHz (用戶可自行修改頻率).
******************************************/
#include "STC32G.h"
#include "stdio.h"
#include "intrins.h"
#define uchar unsigned char//宏定義無符號(hào)字符型
#define uint unsigned int //宏定義無符號(hào)整型
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
#define MAIN_Fosc 22118400L //定義主時(shí)鐘(精確計(jì)算115200波特率)
//==========================================================================
#define Baudrate1 (65536 - MAIN_Fosc / 9600 / 4)
#define Baudrate2 (65536 - MAIN_Fosc / 9600 / 4)
#define UART1_BUF_LENGTH 128
#define UART2_BUF_LENGTH 128
//==========================================================================
/************* 本地常量聲明 **************/
/************* IO口定義 **************/
/************* 本地變量聲明 **************/
u8 TX1_Cnt; //發(fā)送計(jì)數(shù)
u8 RX1_Cnt; //接收計(jì)數(shù)
u8 TX2_Cnt; //發(fā)送計(jì)數(shù)
u8 RX2_Cnt; //接收計(jì)數(shù)
bit B_TX1_Busy; //發(fā)送忙標(biāo)志
bit B_TX2_Busy; //發(fā)送忙標(biāo)志
u8 RX1_Buffer[UART1_BUF_LENGTH]; //接收緩沖
u8 RX2_Buffer[UART2_BUF_LENGTH]; //接收緩沖
uchar receBuf[10];//接收緩沖區(qū)
/************* 本地函數(shù)聲明 **************/
void UART1_config(u8 brt); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
void UART2_config(u8 brt); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
void PrintString1(u8 *puts);
void PrintString2(u8 *puts);
void delay(uchar dat);//延時(shí)程序
void senduart2();//發(fā)送函數(shù)
void KEY(); //按鍵函數(shù)
void LED(); //測(cè)試輸出
/**************** IO口定義及聲明 *****************/
/**************** 按鍵輸入口 *****************/
sbit S1=P3^2;//外部中斷輸入0
sbit S2=P3^3;//外部中斷輸入1
sbit S3=P3^4;
sbit S4=P3^5;
sbit S5=P3^6;
sbit S6=P3^7;
/**************** 485通訊換向口 *****************/
sbit RS485_DIR = P1^4; //RS485方向控制
/**************** 外部函數(shù)聲明和外部變量聲明 *****************/
bit flag_zx2=0;//串口2標(biāo)志
uchar UART2_JS=0;//串口2數(shù)據(jù)計(jì)數(shù)
bit S1bz=0;//按鍵1標(biāo)志
bit S2bz=0;//按鍵2標(biāo)志
bit S3bz=0;//按鍵3標(biāo)志
bit S4bz=0;//按鍵4標(biāo)志
bit S5bz=0;//按鍵5標(biāo)志
bit S6bz=0;//按鍵6標(biāo)志
bit UART2_FSBZ=0;//串口2發(fā)送標(biāo)志
uchar uart_dat=0;//按鍵數(shù)據(jù)
uint yssj=5000;
/******************** 主函數(shù) **************************/
void main(void)
{
WTST = 0; //設(shè)置程序指令延時(shí)參數(shù),賦值為0可將CPU執(zhí)行指令的速度設(shè)置為最快
EAXFR = 1; //擴(kuò)展寄存器(XFR)訪問使能
CKCON = 0; //提高訪問XRAM速度
P0M1 = 0x00; P0M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
P1M1 = 0x00; P1M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
P2M1 = 0x00; P2M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
P3M1 = 0x00; P3M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
P4M1 = 0x00; P4M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
P5M1 = 0x00; P5M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
P6M1 = 0x00; P6M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
P7M1 = 0x00; P7M0 = 0x00; //設(shè)置為準(zhǔn)雙向口
LED();
UART1_config(1); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
UART2_config(2); // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
EA = 1; //允許全局中斷
RS485_DIR=1;//發(fā)送狀態(tài)
PrintString1("STC32G UART1 Test Programme!\r\n"); //UART1發(fā)送一個(gè)字符串
PrintString2("STC32G UART2 Test Programme!\r\n"); //UART2發(fā)送一個(gè)字符串
RS485_DIR=0;//讀取狀態(tài)
while (1)
{
KEY();//按鍵函數(shù)
if((TX1_Cnt != RX1_Cnt) && (!B_TX1_Busy)) //收到數(shù)據(jù), 發(fā)送空閑
{
SBUF = RX1_Buffer[TX1_Cnt];
B_TX1_Busy = 1;
if(++TX1_Cnt >= UART1_BUF_LENGTH) TX1_Cnt = 0;
}
// if((TX2_Cnt != RX2_Cnt) && (!B_TX2_Busy)) //收到數(shù)據(jù), 發(fā)送空閑
// {
//// RS485_DIR=1;//
// S2BUF = RX2_Buffer[TX2_Cnt];
// B_TX2_Busy = 1;
// if(++TX2_Cnt >= UART2_BUF_LENGTH) TX2_Cnt = 0;
//// RS485_DIR=0;//
// }
}
}
//========================================================================
// 主函數(shù)程序結(jié)束
//========================================================================
/********************************************************************
延時(shí)函數(shù)
*********************************************************************/
void delay(uint dat)//延時(shí)程序
{
uint m,n,s;
for(m=dat;m>0;m--)
for(n=2;n>0;n--)
for(s=100;s>0;s--);
}
/****************發(fā)送函數(shù)*********************/
void senduart2()//發(fā)送函數(shù)
{
RS485_DIR=1;
S2BUF=0xAA;B_TX2_Busy = 1;while(B_TX2_Busy);
S2BUF=uart_dat;B_TX2_Busy = 1;while(B_TX2_Busy);
S2BUF=0xFF;B_TX2_Busy = 1;while(B_TX2_Busy);
S2BUF=0xFF;B_TX2_Busy = 1;while(B_TX2_Busy);
S2BUF=0xBB;B_TX2_Busy = 1;while(B_TX2_Busy);
RS485_DIR=0;
}
/*****************清空發(fā)送緩沖區(qū)*************************/
void clear_receBuf() //清空發(fā)送緩沖區(qū)
{
uchar i;
for(i=0;i<5;i++)
{
RX2_Buffer=0;
}
}
/********************************************************************
按鍵函數(shù)
*********************************************************************/
void KEY() //按鍵函數(shù)
{
if(UART2_FSBZ==1){senduart2();UART2_FSBZ=0;}
if((S1==0)&&(S1bz==0)){delay(10);if((S1==0)&&(S1bz==0)){uart_dat=0X01;senduart2();S1bz=1;}}if((S1==1)&&(S1bz==1)){delay(10);if((S1==1)&&(S1bz==1)){S1bz=0;}}
if((S2==0)&&(S2bz==0)){delay(10);if((S2==0)&&(S2bz==0)){uart_dat=0X02;senduart2();S2bz=1;}}if((S2==1)&&(S2bz==1)){delay(10);if((S2==1)&&(S2bz==1)){S2bz=0;}}
if((S3==0)&&(S3bz==0)){delay(10);if((S3==0)&&(S3bz==0)){uart_dat=0X03;senduart2();S3bz=1;}}if((S3==1)&&(S3bz==1)){delay(10);if((S3==1)&&(S3bz==1)){S3bz=0;}}
if((S4==0)&&(S4bz==0)){delay(10);if((S4==0)&&(S4bz==0)){uart_dat=0X04;senduart2();S4bz=1;}}if((S4==1)&&(S4bz==1)){delay(10);if((S4==1)&&(S4bz==1)){S4bz=0;}}
if((S5==0)&&(S5bz==0)){delay(10);if((S5==0)&&(S5bz==0)){uart_dat=0X05;senduart2();S5bz=1;}}if((S5==1)&&(S5bz==1)){delay(10);if((S5==1)&&(S5bz==1)){S5bz=0;}}
if((S6==0)&&(S6bz==0)){delay(10);if((S6==0)&&(S6bz==0)){uart_dat=0X06;senduart2();S6bz=1;}}if((S6==1)&&(S6bz==1)){delay(10);if((S6==1)&&(S6bz==1)){S6bz=0;}}
}
void LED() //測(cè)試輸出
{
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);P40=0;
delay(yssj);P64=0;
delay(yssj);P65=0;
delay(yssj);P66=0;
delay(yssj);P67=0;
delay(yssj);P30=0;
delay(yssj);P31=0;
delay(yssj);P32=0;
delay(yssj);P33=0;
delay(yssj);P34=0;
delay(yssj);P50=0;
delay(yssj);P51=0;
delay(yssj);P35=0;
delay(yssj);P36=0;
delay(yssj);P37=0;
delay(yssj);P70=0;
delay(yssj);P71=0;
delay(yssj);P72=0;
delay(yssj);P73=0;
delay(yssj);P41=0;
delay(yssj);P42=0;
delay(yssj);P43=0;
delay(yssj);P44=0;
delay(yssj);P20=0;
delay(yssj);P21=0;
delay(yssj);P22=0;
delay(yssj);P23=0;
delay(yssj);P24=0;
delay(yssj);P25=0;
delay(yssj);P26=0;
delay(yssj);P27=0;
delay(yssj);P74=0;
delay(yssj);P75=0;
delay(yssj);P76=0;
delay(yssj);P77=0;
delay(yssj);P45=0;
delay(yssj);P46=0;
delay(yssj);P00=0;
delay(yssj);P01=0;
delay(yssj);P02=0;
delay(yssj);P03=0;
delay(yssj);P04=0;
delay(yssj);P52=0;
delay(yssj);P53=0;
delay(yssj);P05=0;
delay(yssj);P06=0;
delay(yssj);P07=0;
delay(yssj);P60=0;
delay(yssj);P61=0;
delay(yssj);P62=0;
delay(yssj);P63=0;
delay(yssj);P10=0;
delay(yssj);P11=0;
delay(yssj);P47=0;
delay(yssj);P14=0;
delay(yssj);P15=0;
delay(yssj);P16=0;
delay(yssj);P17=0;
delay(yssj);P13=0;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);
P0=0X00;
P1=0X00;
P2=0X00;
P3=0X00;
P4=0X00;
P5=0X00;
P6=0X00;
P7=0X00;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);
P0=0X00;
P1=0X00;
P2=0X00;
P3=0X00;
P4=0X00;
P5=0X00;
P6=0X00;
P7=0X00;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
delay(yssj);
P0=0X00;
P1=0X00;
P2=0X00;
P3=0X00;
P4=0X00;
P5=0X00;
P6=0X00;
P7=0X00;
delay(yssj);
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
P4=0XFF;
P5=0XFF;
P6=0XFF;
P7=0XFF;
}
//========================================================================
// 函數(shù): void PrintString1(u8 *puts)
// 描述: 串口1發(fā)送字符串函數(shù)。
// 參數(shù): puts: 字符串指針.
// 返回: none.
//========================================================================
void PrintString1(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0結(jié)束
{
SBUF = *puts;
B_TX1_Busy = 1;
while(B_TX1_Busy);
}
}
//========================================================================
// 函數(shù): void PrintString2(u8 *puts)
// 描述: 串口2發(fā)送字符串函數(shù)。
// 參數(shù): puts: 字符串指針.
// 返回: none.
//========================================================================
void PrintString2(u8 *puts)
{
for (; *puts != 0; puts++) //遇到停止符0結(jié)束
{
RS485_DIR=1;//
S2BUF = *puts;
B_TX2_Busy = 1;
while(B_TX2_Busy);
RS485_DIR=0;//
}
}
//========================================================================
// 函數(shù): SetTimer2Baudraye(u32 dat)
// 描述: 設(shè)置Timer2做波特率發(fā)生器。
// 參數(shù): dat: Timer2的重裝值.
// 返回: none.
//========================================================================
void SetTimer2Baudraye(u32 dat)
{
T2R = 0; //Timer stop
T2_CT = 0; //Timer2 set As Timer
T2x12 = 1; //Timer2 set as 1T mode
T2H = (u8)(dat / 256);
T2L = (u8)(dat % 256);
ET2 = 0; //禁止定時(shí)器中斷
T2R = 1; //Timer run enable
}
//========================================================================
// 函數(shù): void UART1_config(u8 brt)
// 描述: UART1初始化函數(shù)。
// 參數(shù): brt: 選擇波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
// 返回: none.
//========================================================================
void UART1_config(u8 brt)
{
brt = 0;
/*********** 波特率使用定時(shí)器2 *****************/
if(brt == 2)
{
S1BRT = 1; //S1 BRT Use Timer2;
SetTimer2Baudraye(Baudrate1);
}
/*********** 波特率使用定時(shí)器1 *****************/
else
{
TR1 = 0;
S1BRT = 0; //S1 BRT Use Timer1;
T1_CT = 0; //Timer1 set As Timer
T1x12 = 1; //Timer1 set as 1T mode
TMOD &= ~0x30;//Timer1_16bitAutoReload;
TH1 = (u8)(Baudrate1 / 256);
TL1 = (u8)(Baudrate1 % 256);
ET1 = 0; //禁止定時(shí)器中斷
TR1 = 1;
}
/*************************************************/
SCON = (SCON & 0x3f) | 0x40; //UART1模式, 0x00: 同步移位輸出, 0x40: 8位數(shù)據(jù),可變波特率, 0x80: 9位數(shù)據(jù),固定波特率, 0xc0: 9位數(shù)據(jù),可變波特率
// PS = 1; //高優(yōu)先級(jí)中斷
ES = 1; //允許串口中斷
REN = 1; //允許接收
P_SW1 &= 0x3f;
// P_SW1 |= 0x40; //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7, 0xC0: P4.3 P4.4
B_TX1_Busy = 0;
TX1_Cnt = 0;
RX1_Cnt = 0;
}
//========================================================================
// 函數(shù): void UART2_config(u8 brt)
// 描述: UART2初始化函數(shù)。
// 參數(shù): brt: 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
// 返回: none.
//========================================================================
void UART2_config(u8 brt) // 選擇波特率, 2: 使用Timer2做波特率, 其它值: 無效.
{
if(brt == 2)
{
SetTimer2Baudraye(Baudrate2);
S2CFG |= 0x01; //使用串口2時(shí),W1位必需設(shè)置為1,否則可能會(huì)產(chǎn)生不可預(yù)期的錯(cuò)誤
S2CON = (S2CON & 0x3f) | 0x40; //UART2模式, 0x00: 同步移位輸出, 0x40: 8位數(shù)據(jù),可變波特率, 0x80: 9位數(shù)據(jù),固定波特率, 0xc0: 9位數(shù)據(jù),可變波特率
ES2 = 1; //允許中斷
S2REN = 1; //允許接收
S2_S = 0; //UART2 switch to: 0: P1.0 P1.1, 1: P4.6 P4.7
B_TX2_Busy = 0;
TX2_Cnt = 0;
RX2_Cnt = 0;
}
}
//========================================================================
// 函數(shù): void UART1_int (void) interrupt UART1_VECTOR
// 描述: UART1中斷函數(shù)。
// 參數(shù): nine.
// 返回: none.
//========================================================================
void UART1_int (void) interrupt 4
{
if(RI)
{
RI = 0;
RX1_Buffer[RX1_Cnt] = SBUF;
if(++RX1_Cnt >= UART1_BUF_LENGTH) RX1_Cnt = 0;
}
if(TI)
{
TI = 0;
B_TX1_Busy = 0;
}
}
//========================================================================
// 函數(shù): void UART2_int (void) interrupt UART2_VECTOR
// 描述: UART2中斷函數(shù)。
// 參數(shù): nine.
// 返回: none.
//========================================================================
void UART2_int (void) interrupt 8
{
if(S2RI)
{
S2RI=0; //接收標(biāo)志清零
RX2_Buffer[UART2_JS++&0x0F] = S2BUF; //把接受的數(shù)據(jù)存儲(chǔ)到BUT數(shù)組中
if(RX2_Buffer[0]!=0xaa){UART2_JS=0;}
if(UART2_JS>=5){UART2_JS=0;flag_zx2=1;}
}
if(flag_zx2==1)
{
flag_zx2=0;
if((RX2_Buffer[0]==0xaa)&&(RX2_Buffer[1]==0x00)&&(RX2_Buffer[2]==0x00)&&(RX2_Buffer[3]==0x00)&&(RX2_Buffer[4]==0xBB))
{
// P0=0X55;
// senduart2();//發(fā)送數(shù)據(jù)
UART2_FSBZ=1;
}
clear_receBuf();
}
if(S2TI)
{
S2TI = 0; //Clear Tx flag
B_TX2_Busy = 0;
}
}
//========================================================================
// 程序結(jié)束
//========================================================================
|
|