P2M0 = P2M0&0x00|0x0f;
时间: 2023-11-23 09:56:26 浏览: 52
这是对P2M0寄存器进行设置的代码,将P2M0寄存器的低4位设置为0x0f,高4位不变。具体来说,它使用了位运算符&和|,&运算符可以将P2M0寄存器的低4位与0x00进行按位与运算,将结果赋值给P2M0,这样就将低4位清零了;|运算符可以将P2M0寄存器的低4位与0x0f进行按位或运算,将结果赋值给P2M0,这样就将低4位设置为0x0f了。
相关问题
#include "reg51.h" #include "intrins.h" #define FOSC 11059200UL #define BRT (65536 - FOSC / 115200 / 4) sfr AUXR = 0x8e; sfr T2H = 0xd6; sfr T2L = 0xd7; sfr S2CON = 0x9a; sfr S2BUF = 0x9b; sfr IE2 = 0xaf; sfr P0M1 = 0x93; sfr P0M0 = 0x94; sfr P1M1 = 0x91; sfr P1M0 = 0x92; sfr P2M1 = 0x95; sfr P2M0 = 0x96; sfr P3M1 = 0xb1; sfr P3M0 = 0xb2; sfr P4M1 = 0xb3; sfr P4M0 = 0xb4; sfr P5M1 = 0xc9; sfr P5M0 = 0xca; bit busy; char wptr; char rptr; char buffer[16]; void Uart2Isr() interrupt 8 { if (S2CON & 0x02) { S2CON &= ~0x02; busy = 0; } if (S2CON & 0x01) { S2CON &= ~0x01; buffer[wptr++] = S2BUF; wptr &= 0x0f; } } void Uart2Init() { S2CON = 0x10; T2L = BRT; T2H = BRT >> 8; AUXR = 0x14; wptr = 0x00; rptr = 0x00; busy = 0; } void Uart2Send(char dat) { while (busy); busy = 1; S2BUF = dat; } void Uart2SendStr(char *p) { while (*p) { Uart2Send(*p++); } } void main() { P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; Uart2Init(); IE2 = 0x01; EA = 1; Uart2SendStr("Uart Test !\r\n"); while (1) { if (rptr != wptr) { Uart2Send(buffer[rptr++]); rptr &= 0x0f; } } } 修改此程序使发送者发送字母o接收者连接的板子中端口P2.5灯开始亮其余灯灭
修改后的程序如下:
```c
#include "reg51.h"
#include "intrins.h"
#define FOSC 11059200UL
#define BRT (65536 - FOSC / 115200 / 4)
sfr AUXR = 0x8e;
sfr T2H = 0xd6;
sfr T2L = 0xd7;
sfr S2CON = 0x9a;
sfr S2BUF = 0x9b;
sfr IE2 = 0xaf;
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xc9;
sfr P5M0 = 0xca;
bit busy;
char wptr;
char rptr;
char buffer[16];
void Uart2Isr() interrupt 8 {
if (S2CON & 0x02) {
S2CON &= ~0x02;
busy = 0;
}
if (S2CON & 0x01) {
S2CON &= ~0x01;
buffer[wptr++] = S2BUF;
wptr &= 0x0f;
}
}
void Uart2Init() {
S2CON = 0x10;
T2L = BRT;
T2H = BRT >> 8;
AUXR = 0x14;
wptr = 0x00;
rptr = 0x00;
busy = 0;
}
void Uart2Send(char dat) {
while (busy);
busy = 1;
S2BUF = dat;
}
void Uart2SendStr(char *p) {
while (*p) {
Uart2Send(*p++);
}
}
void main() {
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
Uart2Init();
IE2 = 0x01;
EA = 1;
Uart2SendStr("Uart Test !\r\n");
while (1) {
if (rptr != wptr) {
char received = buffer[rptr++];
rptr &= 0x0f;
if (received == 'o') {
P2 = 0x20;
} else {
P2 = 0x00;
}
Uart2Send(received);
}
}
}
```
其中,修改的部分为:
```c
if (received == 'o') {
P2 = 0x20;
} else {
P2 = 0x00;
}
```
该部分代码会检测接收到的字符是否为字母'o',如果是,则点亮P2.5灯(二进制为0010 0000),否则将P2口置为0(灭灯)。
#include “config.h” void Port_Init();位 LED = P2^0;void main() { Port_Init(); // Timer0_Init();Uart1_Init();Uart1String(“hello1\n”);指示灯 = 0;clrStruct();while(1) { parseGpsBuffer(); printGpsBuffer(); } }void Port_Init() { P1M1=0x00;P1M0=0x00;P0M1=0x00;P0M0=0x00;P2M1=0x00;P2M0=0x00;P3M1=0x00;P3M0=0x00;}在这个函数里面添加一个液晶显示显示经纬度
好的,你需要添加一个LCD1602显示屏,然后连接到微控制器上,并且需要一个LCD1602的驱动程序。以下是一个简单的LCD1602驱动程序示例:
```
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
sbit LCD_D4 = P0^4;
sbit LCD_D5 = P0^5;
sbit LCD_D6 = P0^6;
sbit LCD_D7 = P0^7;
void delay_ms(unsigned int ms)
{
unsigned int i, j;
for (i = 0; i < ms; i++)
{
for (j = 0; j < 120; j++);
}
}
void LCD_WriteCommand(unsigned char cmd)
{
RS = 0;
RW = 0;
LCD_D7 = cmd & 0x80;
LCD_D6 = cmd & 0x40;
LCD_D5 = cmd & 0x20;
LCD_D4 = cmd & 0x10;
EN = 1;
delay_ms(1);
EN = 0;
delay_ms(1);
LCD_D7 = cmd & 0x08;
LCD_D6 = cmd & 0x04;
LCD_D5 = cmd & 0x02;
LCD_D4 = cmd & 0x01;
EN = 1;
delay_ms(1);
EN = 0;
delay_ms(1);
}
void LCD_WriteData(unsigned char dat)
{
RS = 1;
RW = 0;
LCD_D7 = dat & 0x80;
LCD_D6 = dat & 0x40;
LCD_D5 = dat & 0x20;
LCD_D4 = dat & 0x10;
EN = 1;
delay_ms(1);
EN = 0;
delay_ms(1);
LCD_D7 = dat & 0x08;
LCD_D6 = dat & 0x04;
LCD_D5 = dat & 0x02;
LCD_D4 = dat & 0x01;
EN = 1;
delay_ms(1);
EN = 0;
delay_ms(1);
}
void LCD_Init()
{
LCD_WriteCommand(0x28); // 16×2显示,5×7点阵,4位数据接口
LCD_WriteCommand(0x0C); // 开启显示,不显示光标,不闪烁
LCD_WriteCommand(0x06); // 光标移动时不要移动屏幕
LCD_WriteCommand(0x01); // 清屏
}
void LCD_DisplayString(unsigned char x, unsigned char y, unsigned char *s)
{
unsigned char i = 0;
if (x < 16)
{
x |= 0x80;
switch (y)
{
case 0:
i = 0;
break;
case 1:
i = 0x40;
break;
case 2:
i = 0x10;
break;
case 3:
i = 0x50;
break;
}
i += x;
LCD_WriteCommand(i);
}
while (*s)
{
LCD_WriteData(*s++);
}
}
void main()
{
Port_Init();
Uart1_Init();
LCD_Init();
Uart1String("hello1\n");
while (1)
{
parseGpsBuffer();
printGpsBuffer();
// 在LCD1602显示经纬度
LCD_DisplayString(0, 0, "Lat: "); // 在第一行显示Lat:
LCD_DisplayString(5, 0, Latitude); // 在第一行显示经度
LCD_DisplayString(0, 1, "Lon: "); // 在第二行显示Lon:
LCD_DisplayString(5, 1, Longitude);// 在第二行显示纬度
}
}
```
这样,当你运行程序时,LCD1602将显示经纬度。注意,在代码中的parseGpsBuffer()和printGpsBuffer()函数中,需要将经纬度分别存储在变量Latitude和Longitude中。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)