Adonai
13.04.2007, 15:36
Hallo,
Ich versuche seit einiger Zeit das USART-Modul des PIC18F252 zu nutzen.
Ich möchte eine Kommunikation zwischen 2 PICs aufbauen.
Der Sende-PIC scheint dabei richtig zu funktionieren.
Mit dem Empfangen habe ich noch einige Probleme.
Ich programmiere mit C.
Kann mir da jemand helfen?
Das Sendeprogramm:
//Senden
#include <P18F252.h>
int ring;
void open_serial (void)
{
// Configure UART serial transmit
// Configured for:
// 9600 Baud
// 8N1
// SPBRG - Baud Rate Generator Register
SPBRG = 63; // 20MHz => 9600 baud (BRGH = 0)
// BRGH - High Baud Rate Select Bit
TXSTAbits.BRGH = 0; // (0 = low speed)
// SYNC - USART Mode select Bit
TXSTAbits.SYNC = 0; // (0 = asynchronous)
// TRISC - Tri-state Data Direction Register for port C
// RC6 - 6th pin of port C - used for Serial Transmit
// RC7 - 7th pin of port C - used for Serial Receive
TRISCbits.TRISC6 = 0; // (0 = pin set as output)
TRISCbits.TRISC7 = 1; // (1 = pin set as input)
// SPEN - Serial Port Enable Bit
RCSTAbits.SPEN = 1; // (1 = serial port enabled)
// TXIE - USART Transmit Interupt Enable Bit
PIE1bits.TXIE = 0; // (1 = enabled)
// RCIE - USART Receive Interupt Enable Bit
PIE1bits.RCIE = 0; // (1 = enabled)
// TX9 - 9-bit Transmit Enable Bit
TXSTAbits.TX9 = 0; // (0 = 8-bit transmit)
// RX9 - 9-bit Receive Enable Bit
RCSTAbits.RX9 = 0; // (0 = 8-bit reception)
// CREN - Continuous Receive Enable Bit
RCSTAbits.CREN = 0; // (1 = Enables receiver)
// TXEN - Trasmit Enable Bit
TXSTAbits.TXEN = 1; // (1 = transmit enabled)
// GIE - Global Interrupt Enable Bit
INTCONbits.GIE = 1; // (1 = Enable all unmasked interrupts)
// PEIE - Peripheral Interrupt Enable Bit
INTCONbits.PEIE = 1; // (1 = Enable all unmasked peripheral interrupts)
}
void wait (void)
{
int k;
for(k=0;k<5000;k++);
}
void main (void)
{
ADCON1 = 7;
LATA = 0x00;
TRISA = 0x00;
TRISB = 0xFF;
ring = 0;
while(1)
{
wait();
open_serial();
ring = PORTB;
PORTA = ring;
while(PIR1bits.TXIF)
{
TXREG = ring;
}
}
}
Das Empfangsprogramm:
//Empfangen
#include <P18F252.h>
void open_serial(void)
{
// Configure UART serial transmit
// Configured for:
// 9600 Baud
// 8N1
// SPBRG - Baud Rate Generator Register
SPBRG = 63; // 20MHz => 9600 baud (BRGH = 0)
// BRGH - High Baud Rate Select Bit
TXSTAbits.BRGH = 1; // (0 = low speed)
// SYNC - USART Mode select Bit
TXSTAbits.SYNC = 0; // (0 = asynchronous)
// TRISC - Tri-state Data Direction Register for port C
// RC6 - 6th pin of port C - used for Serial Transmit
// RC7 - 7th pin of port C - used for Serial Receive
TRISCbits.TRISC6 = 0; // (0 = pin set as out)
TRISCbits.TRISC7 = 1; // (1 = pin set as input)
// SPEN - Serial Port Enable Bit
RCSTAbits.SPEN = 1; // (1 = serial port enabled)
// TXIE - USART Transmit Interupt Enable Bit
PIE1bits.TXIE = 0; // (1 = enabled)
// RCIE - USART Receive Interupt Enable Bit
PIE1bits.RCIE = 1; // (1 = enabled)
// TX9 - 9-bit Transmit Enable Bit
TXSTAbits.TX9 = 0; // (0 = 8-bit transmit)
// RX9 - 9-bit Receive Enable Bit
RCSTAbits.RX9 = 0; // (0 = 8-bit reception)
// CREN - Continuous Receive Enable Bit
RCSTAbits.CREN = 1; // (1 = Enables receiver)
// TXEN - Trasmit Enable Bit
TXSTAbits.TXEN = 0; // (1 = transmit enabled)
// GIE - Global Interrupt Enable Bit
INTCONbits.GIE = 0; // (1 = Enable all unmasked interrupts)
// PEIE - Peripheral Interrupt Enable Bit
INTCONbits.PEIE = 1; // (1 = Enable all unmasked peripheral interrupts)
}
void main (void)
{
TRISB = 0x00;
PORTB = 0b01010101;
RCREG = 0;
open_serial();
while(1)
{
if(PIR1bits.RCIF == 1)
{
PORTB = RCREG;
PIR1bits.RCIF = 0;
}
}
}
Danke für die Hilfe. mfg Adonai
Ich versuche seit einiger Zeit das USART-Modul des PIC18F252 zu nutzen.
Ich möchte eine Kommunikation zwischen 2 PICs aufbauen.
Der Sende-PIC scheint dabei richtig zu funktionieren.
Mit dem Empfangen habe ich noch einige Probleme.
Ich programmiere mit C.
Kann mir da jemand helfen?
Das Sendeprogramm:
//Senden
#include <P18F252.h>
int ring;
void open_serial (void)
{
// Configure UART serial transmit
// Configured for:
// 9600 Baud
// 8N1
// SPBRG - Baud Rate Generator Register
SPBRG = 63; // 20MHz => 9600 baud (BRGH = 0)
// BRGH - High Baud Rate Select Bit
TXSTAbits.BRGH = 0; // (0 = low speed)
// SYNC - USART Mode select Bit
TXSTAbits.SYNC = 0; // (0 = asynchronous)
// TRISC - Tri-state Data Direction Register for port C
// RC6 - 6th pin of port C - used for Serial Transmit
// RC7 - 7th pin of port C - used for Serial Receive
TRISCbits.TRISC6 = 0; // (0 = pin set as output)
TRISCbits.TRISC7 = 1; // (1 = pin set as input)
// SPEN - Serial Port Enable Bit
RCSTAbits.SPEN = 1; // (1 = serial port enabled)
// TXIE - USART Transmit Interupt Enable Bit
PIE1bits.TXIE = 0; // (1 = enabled)
// RCIE - USART Receive Interupt Enable Bit
PIE1bits.RCIE = 0; // (1 = enabled)
// TX9 - 9-bit Transmit Enable Bit
TXSTAbits.TX9 = 0; // (0 = 8-bit transmit)
// RX9 - 9-bit Receive Enable Bit
RCSTAbits.RX9 = 0; // (0 = 8-bit reception)
// CREN - Continuous Receive Enable Bit
RCSTAbits.CREN = 0; // (1 = Enables receiver)
// TXEN - Trasmit Enable Bit
TXSTAbits.TXEN = 1; // (1 = transmit enabled)
// GIE - Global Interrupt Enable Bit
INTCONbits.GIE = 1; // (1 = Enable all unmasked interrupts)
// PEIE - Peripheral Interrupt Enable Bit
INTCONbits.PEIE = 1; // (1 = Enable all unmasked peripheral interrupts)
}
void wait (void)
{
int k;
for(k=0;k<5000;k++);
}
void main (void)
{
ADCON1 = 7;
LATA = 0x00;
TRISA = 0x00;
TRISB = 0xFF;
ring = 0;
while(1)
{
wait();
open_serial();
ring = PORTB;
PORTA = ring;
while(PIR1bits.TXIF)
{
TXREG = ring;
}
}
}
Das Empfangsprogramm:
//Empfangen
#include <P18F252.h>
void open_serial(void)
{
// Configure UART serial transmit
// Configured for:
// 9600 Baud
// 8N1
// SPBRG - Baud Rate Generator Register
SPBRG = 63; // 20MHz => 9600 baud (BRGH = 0)
// BRGH - High Baud Rate Select Bit
TXSTAbits.BRGH = 1; // (0 = low speed)
// SYNC - USART Mode select Bit
TXSTAbits.SYNC = 0; // (0 = asynchronous)
// TRISC - Tri-state Data Direction Register for port C
// RC6 - 6th pin of port C - used for Serial Transmit
// RC7 - 7th pin of port C - used for Serial Receive
TRISCbits.TRISC6 = 0; // (0 = pin set as out)
TRISCbits.TRISC7 = 1; // (1 = pin set as input)
// SPEN - Serial Port Enable Bit
RCSTAbits.SPEN = 1; // (1 = serial port enabled)
// TXIE - USART Transmit Interupt Enable Bit
PIE1bits.TXIE = 0; // (1 = enabled)
// RCIE - USART Receive Interupt Enable Bit
PIE1bits.RCIE = 1; // (1 = enabled)
// TX9 - 9-bit Transmit Enable Bit
TXSTAbits.TX9 = 0; // (0 = 8-bit transmit)
// RX9 - 9-bit Receive Enable Bit
RCSTAbits.RX9 = 0; // (0 = 8-bit reception)
// CREN - Continuous Receive Enable Bit
RCSTAbits.CREN = 1; // (1 = Enables receiver)
// TXEN - Trasmit Enable Bit
TXSTAbits.TXEN = 0; // (1 = transmit enabled)
// GIE - Global Interrupt Enable Bit
INTCONbits.GIE = 0; // (1 = Enable all unmasked interrupts)
// PEIE - Peripheral Interrupt Enable Bit
INTCONbits.PEIE = 1; // (1 = Enable all unmasked peripheral interrupts)
}
void main (void)
{
TRISB = 0x00;
PORTB = 0b01010101;
RCREG = 0;
open_serial();
while(1)
{
if(PIR1bits.RCIF == 1)
{
PORTB = RCREG;
PIR1bits.RCIF = 0;
}
}
}
Danke für die Hilfe. mfg Adonai