UART mit dem F330 von SiLabs
Hallo
Ich hoffe ihr könnt mir weiter helfen. Ich möchte mit dem F330 von SiLabs eine RS232 Kommunikation aufbauen. Ich habe dazu folgendes Programm geschrieben:
Code:
#include <c8051F330.h>
#include <stdio.h>
#define SYSCLK 24500000
#define BAUDRATE 115200
#define RX_length 25
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
//bit TX_ready; //True = OK zur Übertragung
//char *TX_ptr; //pointer auf zu übermittelnden String
bit RX_ready; //True = string empfangen
char RX_buf[RX_length]; //Array als Eingangsbuffer anlegen
unsigned char the_char; //zwischenspeicher
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void UART0_ISR (void);
void Print_String (char pstring[]);
//-----------------------------------------------------------------------------
// main Routine
//-----------------------------------------------------------------------------
void main (void)
{
// Disable Watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
OSCILLATOR_Init (); // Initialize Oscillator
PORT_Init(); // Initialize Port I/O
UART0_Init (); // Initialize UART0
EA = 0; //Interrupts global freigeben
ES0 = 1; //UART0 interrupt freigeben
while (1)
{
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine initializes the system clock to use the internal 24.5MHz
// oscillator as its clock source. Also enables missing clock detector reset.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency
RSTSRC = 0x04; // Enable missing clock detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// P0.4 digital push-pull UART TX
// P0.5 digital open-drain UART RX
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P0SKIP |= 0x01; // Skip P0.0 for external VREF
P1MDIN |= 0xEF; // Configure P1.4 as analog input.
P0MDOUT |= 0x10; // enable UTX as push-pull output
XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
XBR1 = 0x40; // Enable crossbar and weak pull-ups
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x09;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1; // Init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
void UART0_ISR (void) interrupt 4 using 3
{
static unsigned char RX_index = 0; //receive buffer index
unsigned char the_char; //zwischenspeicher
Print_String ("In ISR..."); //Debugausgabe auf UART
RI0 = 0;
if (RI0=1)
{
the_char = SBUF0;
RX_buf[RX_index] = the_char; //Byte in array speichern
if(RX_index < (RX_length-2))
{
RX_index++;
}
else
{
RX_index = 0;
RX_ready = 1;
RX_buf[RX_index-1] = '\0';
}
}
}
void Print_String (char pstring[])
{
unsigned char i = 0;
while (pstring[i])
{
putchar(pstring[i++]);
}
}
Das Senden eines Strings funktioniert auch schon gut. Ich möchte nun aber auch einen String einlesen, speichern und je nach dem was für Kommandos angekommen sind eine Aktion ausführen.
Im Moment sieht es so aus als wenn der Controller nicht einmal in die UART ISR springt. Um das zu überprüfen gebe ich den Text "In ISR..." aus. Der Text wird ausgegeben wenn ich in main das Bit RI0 manuell setze. Eigentlich sollte das Bit automatisch gesetzt werden wenn ein byte vom PC empfangen wurde. :-k
Wie gesagt, das Senden von Text an den PC funtkioniert. Liegt also denke ich nicht an der Baudrate oder gleichen. Probier jetzt schon ne ganze Weile rum, komme aber nicht weiter. Hoffe ihr habt eine Idee.
Grüße!
Bean