完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
如题,定时器0用来模拟pwm了,定时器1用于超声波模块避障当计数器了,现在在做一辆循迹避障智能小车,还需要用到串口通信,请问这种情况下还能进行串口通信吗。小白初学不懂,求大佬指教一下
|
|
相关推荐
2个回答
|
|
串口有没有用到定时器呢,如果没有,那么可以,如果串口用到了定时器,比如定时器1,那么需要再定时器1中包含串口的代码支持
|
|
|
|
如果是STC89C51 STC89C52的话,可以用定时器2做串口波特率发生器,#include "reg51.h"
#include "intrins.h" sfr T2CON = 0xC8; //timer2 control register sfr RCAP2L = 0xCA; sfr RCAP2H = 0xCB; sfr TL2 = 0xCC; sfr TH2 = 0xCD; typedef unsigned char BYTE; typedef unsigned int WORD; #define FOSC 11059200L //System frequency #define BAUD 115200 //UART baudrate /*Define UART parity mode*/ #define NONE_PARITY 0 //None parity #define ODD_PARITY 1 //Odd parity #define EVEN_PARITY 2 //Even parity #define MARK_PARITY 3 //Mark parity #define SPACE_PARITY 4 //Space parity #define PARITYBIT EVEN_PARITY //Testing even parity ***it bit9 = P2^2; //P2.2 show UART data bit9 bit busy; void SendData(BYTE dat); void SendString(char *s); void main() { #if (PARITYBIT == NONE_PARITY) SCON = 0x50; //8-bit variable UART #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY) SCON = 0xda; //9-bit variable UART, parity bit initial to 1 #elif (PARITYBIT == SPACE_PARITY) SCON = 0xd2; //9-bit variable UART, parity bit initial to 0 #endif TL2 = RCAP2L = (65536-(FOSC/32/BAUD)); //Set auto-reload vaule TH2 = RCAP2H = (65536-(FOSC/32/BAUD)) >> 8; T2CON = 0x34; //Timer2 start run ES = 1; //Enable UART interrupt EA = 1; //Open master interrupt switch SendString("STC89-90xxrnUart Test !rn"); while(1); } /*---------------------------- UART interrupt service routine ----------------------------*/ void Uart_Isr() interrupt 4 { if (RI) { RI = 0; //Clear receive interrupt flag P0 = SBUF; //P0 show UART data bit9 = RB8; //P2.2 show parity bit } if (TI) { TI = 0; //Clear transmit interrupt flag busy = 0; //Clear transmit busy flag } } /*---------------------------- Send a byte data to UART Input: dat (data to be sent) Output:None ----------------------------*/ void SendData(BYTE dat) { while (busy); //Wait for the completion of the previous data is sent ACC = dat; //Calculate the even parity bit P (PSW.0) if (P) //Set the parity bit according to P { #if (PARITYBIT == ODD_PARITY) TB8 = 0; //Set parity bit to 0 #elif (PARITYBIT == EVEN_PARITY) TB8 = 1; //Set parity bit to 1 #endif } else { #if (PARITYBIT == ODD_PARITY) TB8 = 1; //Set parity bit to 1 #elif (PARITYBIT == EVEN_PARITY) TB8 = 0; //Set parity bit to 0 #endif } busy = 1; SBUF = ACC; //Send data to UART buffer } /*---------------------------- Send a string to UART Input: s (address of string) Output:None ----------------------------*/ void SendString(char *s) { while (*s) //Check the end of the string { SendData(*s++); //Send current char and increment string ptr } } |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
298 浏览 1 评论
《DNESP32S3使用指南-IDF版_V1.6》第二十六章 INFRARED_RECEPTION实验
309 浏览 0 评论
826 浏览 0 评论
求助一下关于51系列单片机的Timer0的计时问题,TH0、TL0+1的时间是怎么算的?
1911 浏览 2 评论
【RA-Eco-RA4E2-64PIN-V1.0开发板试用】开箱+Keil环境搭建+点灯+点亮OLED
1427 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
12032 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 10:14 , Processed in 0.400306 second(s), Total 41, Slave 34 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号