【发布时间】:2019-04-10 15:12:35
【问题描述】:
我已经更改了接收器的代码,但发送器几乎相同,所以在接收器中我取消了中断,而是在循环中读取数据,每次都检查状态RC2IF_bit 并且检查是按照发送器发送数据的相同顺序进行的,以确保哪个端口正在发送数据。
问题出在 proteus 上的模拟中,当我收到数据并将其输出到 LED 上时,它们工作正常并且数据发送正确,但是在模拟过程中,当我观看它时,我发现它们一目了然地改变了状态,例如,如果我发送PORTA = 0X2D 我收到相同的值并且正确的 LED 开启,但一秒钟后它们关闭然后再次开启,这是编写的代码中的问题。
/*This is the transmitter code*/
void UART2_TX_init (); //UART2 transmission initialization function porototype
void SEND_data (int output); //Send data function prototype
void main()
{
ANSELA = 0X00; //Disable analogue function on PORTA
ANSELE = 0X00; //Disable analogue function on PORTE
ANSELF = 0X00; //Disable analogue function on PORTF
TRISA = 0XFF; // SET PORTA AS INPUT (AUTOMATIC BUTTONS)
TRISB = 0XFF; //SET PORTB AS INPUT (OFF BUTTONS)
TRISD = 0XFF; //SET PORTD AS INPUT (MANUAL BUTTONS)
TRISE = 0XFF; //SET PORTE AS INPUT (HIGH BUTTONS)
TRISF = 0XFF; //SET PORTF AS INPUT (LOW BUTTONS)
TRISC = 0XFF; //SET PORTC AS INPUT (TRIP BUTTONS)
TRISG0_bit = 0; //set PORTC pin0 as output for MAX487 DE pin
PORTG.F0= 1; //set PORTC pin0 HIGH , set MAX487 as transmitter.
UART2_TX_init(); //call UART1 transmission initialization
while (1)
{
SEND_data(PORTA); //send data of automatic
delay_ms(10);
SEND_data(PORTB); //send data of off
delay_ms(10);
SEND_data(PORTD); //send daata of manual
delay_ms(10);
SEND_data(PORTE); //send data of high
delay_ms(10);
SEND_data(PORTF); //send data of low
delay_ms(10);
SEND_data(PORTC); //send data of TRIP
delay_ms(10);
}
}
/*This function takes the data needed to be send
as an integer. Wait for the TSR to be empty and
start the transmission*/
void SEND_data (int output)
{
while (!TRMT_TX2STA_bit){}; //checks if TSR is empty or not if empty TRMT_BIT is set and write to transmit register
TX2REG = output; //write data to be send in the transmission register
}
/*This function initializes the UART2 as an asynchronous
transmitter at a baud rate of 9600kbps*/
void UART2_TX_init ()
{
BAUD2CON = 0X08;
BRGH_TX2STA_bit = 1;
SP2BRGL = 207;
SYNC_TX2STA_bit = 0X00;
SPEN_RC2STA_bit = 0X01;
TRISG1_bit = 0X01;
TRISG2_bit = 0X01;
TXEN_TX2STA_bit = 0X01;
}
/*This is the receiver code*/
void UART2_RX_init (); // Receiver initialization function prototype
void main()
{
ANSELA = 0X00; // Disable analog function on PORTA
ANSELE = 0X00; // Disable analog function on PORTE
ANSELF = 0X00; // Disable analog function on PORTF
TRISA = 0X00; //set PORTA as output
TRISB = 0X00; //set PORTB as output
TRISD = 0X00; //set PORTD as output
TRISE = 0X00; //set PORTE as output
TRISF = 0X00; //set PORTF as output
TRISC = 0X00; //set PORTF as output
PORTA = 0x00; //clear PORTA
PORTB = 0x00; //clear PORTB
PORTD = 0x00; //clear PORTD
PORTE = 0x00; //clear PORTE
PORTF = 0x00; //clear PORTF
PORTC = 0x00; //clear PORTC
TRISG0_bit = 0x00; //set PORTC pin0 as output for MAX487 RE pin
PORTG.F0 = 0x00; // set PORTC pin0 as LOW , set MAX487 as receiver
UART2_RX_init(); //call receiver initialization function
while (1)
{
while (!RC2IF_bit) ; //check if the RCIF flag is up to tell if there is a new data
PORTA =~ RC2REG; //write the new data to the specified port
while (!RC2IF_bit) ;
PORTB =~ RC2REG;
while (!RC2IF_bit) ;
PORTD =~ RC2REG;
while (!RC2IF_bit) ;
PORTE =~ RC2REG;
while (!RC2IF_bit) ;
PORTF =~ RC2REG;
while (!RC2IF_bit) ;
PORTC =~ RC2REG;
}
}
/*This function initializes UART2 as an asynchronous
receiver at a baud rate of 9600kbps with continous
reception NO INTERRUPT ON RECEIVE*/
void UART2_RX_init ()
{
BAUD2CON = 0X08;
BRGH_TX2STA_bit = 1;
SP2BRGL = 207;
SYNC_TX2STA_bit = 0X00;
SPEN_RC2STA_bit = 0X01;
TRISG1_bit = 0X01;
TRISG2_bit = 0X01;
CREN_RC2STA_bit = 0X01;
}
【问题讨论】:
-
请在您的问题中添加更多详细信息,会发生什么以及您希望发生什么。 “在其他 PIC 上收到的输出似乎闪烁并且不稳定”我不清楚。只是在不知道具体控制器的情况下的一些想法:
interrupt的行为可能取决于时间。if (RCIF_bit ==1)是什么意思?确切/接收缓冲区中至少有一个字节可用?假设RCREG读取最后收到的字节。有先进先出吗?如果还没有新数据怎么办?也许在interrupt中,您应该只读取 1 个字节并使用静态计数器来决定哪个PORTA..F发送这个字节。 -
整个想法是我想从一些开关读取输入,每个端口上有五个开关,然后通过 UART 将此数据发送到另一个 PIC,在其中打开相应的位对应的端口。当我为任意三个端口传输数据时,程序运行良好,但是当我添加另一个端口时,接收器上的 LED 一直闪烁,并且正在传输的数据没有被正确接收。
-
永远永远永远不要延迟接收中断。中断程序应尽可能短。
-
@aymanmagdy 您应该edit 您的问题以添加更多信息,而不是在 cmets 中回答。我的问题也旨在暗示您应该思考什么。正如其他人已经在答案中写的那样,您的协议需要同步。您的接收器实现可能会多次读取相同的数据,这将导致数据被写入错误的端口。
标签: c microcontroller pic uart