list p=16f877A ; list directive to define processor
#include <p16f877A.inc> ; processor specific variable definitions
errorlevel -302, -207
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF
bank0 macro
bcf 03h,5
bcf 03h,6 ; ram page 0
endm
bank1 macro
bsf 03h,5
bcf 03h,6 ; ram page 1
endm
;---------------------------------------------------------------------------
;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set
bank0
bsf 18h,7
bsf 07h,6
bank1
movlw 00h
movwf 88h
bsf 87h,7
;---------------------------------------------------------------------------
;1. initialize the spbrg register for the appropriate
;baud rate. if a high speed baud rate is desired,
;set bit brgh (section 10.1).
bank1
bsf 98h,2
movlw 0ch ;baud rate = 19,2 k
movwf 99h ;4 mhz
;---------------------------------------------------------------------------
;2. enable the asynchronous serial port by clearing
;bit sync and setting bit spen.
bank1
bcf 98h,4
bank0
bsf 18h,7
;---------------------------------------------------------------------------
;3. If interrupts are desired, then set enable bit
;RCIE..
bank1
bcf 8ch,5
;---------------------------------------------------------------------------
;4. 4. If 9-bit reception is desired, then set bit RX9.
bank0
bcf 18h,6
;---------------------------------------------------------------------------
; 5. Enable the reception by setting bit CREN.
bank0
bsf 18h,4
;---------------------------------------------------------------------------
;6. Flag bit RCIF will be set when reception is complete
;and an interrupt will be generated if enable
;bit RCIE is set.
;---------------------------------------------------------------------------
;7. Read the RCSTA register to get the ninth bit (if
;enabled) and determine if any error occurred
;during reception.
;---------------------------------------------------------------------------
;8. Read the 8-bit received data by reading the
;RCREG register.
vrti
bank0
movf RCREG,0 ;byte that I send to LED on PORTD
movwf PORTD ;txreg
;bank1
cekaj btfss PIR1,5
goto cekaj
goto vrti
;---------------------------------------------------------------------------
;8. if using interrupts, ensure that gie and peie
;(bits 7 and 6) of the intcon register are set.
end
This should give you a good idea of how to do this The code is by ericgibbs
You'll have to change to match your carder baud rate
Rich (BB code):list p=16f877A ; list directive to define processor #include <p16f877A.inc> ; processor specific variable definitions errorlevel -302, -207 __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF bank0 macro bcf 03h,5 bcf 03h,6 ; ram page 0 endm bank1 macro bsf 03h,5 bcf 03h,6 ; ram page 1 endm ;--------------------------------------------------------------------------- ;bit spen (rcsta<7>) and bits trisc<7:6> have tobe set bank0 bsf 18h,7 bsf 07h,6 bank1 movlw 00h movwf 88h bsf 87h,7 ;--------------------------------------------------------------------------- ;1. initialize the spbrg register for the appropriate ;baud rate. if a high speed baud rate is desired, ;set bit brgh (section 10.1). bank1 bsf 98h,2 movlw 0ch ;baud rate = 19,2 k movwf 99h ;4 mhz ;--------------------------------------------------------------------------- ;2. enable the asynchronous serial port by clearing ;bit sync and setting bit spen. bank1 bcf 98h,4 bank0 bsf 18h,7 ;--------------------------------------------------------------------------- ;3. If interrupts are desired, then set enable bit ;RCIE.. bank1 bcf 8ch,5 ;--------------------------------------------------------------------------- ;4. 4. If 9-bit reception is desired, then set bit RX9. bank0 bcf 18h,6 ;--------------------------------------------------------------------------- ; 5. Enable the reception by setting bit CREN. bank0 bsf 18h,4 ;--------------------------------------------------------------------------- ;6. Flag bit RCIF will be set when reception is complete ;and an interrupt will be generated if enable ;bit RCIE is set. ;--------------------------------------------------------------------------- ;7. Read the RCSTA register to get the ninth bit (if ;enabled) and determine if any error occurred ;during reception. ;--------------------------------------------------------------------------- ;8. Read the 8-bit received data by reading the ;RCREG register. vrti bank0 movf RCREG,0 ;byte that I send to LED on PORTD movwf PORTD ;txreg ;bank1 cekaj btfss PIR1,5 goto cekaj goto vrti ;--------------------------------------------------------------------------- ;8. if using interrupts, ensure that gie and peie ;(bits 7 and 6) of the intcon register are set. end
Oh my bad...I am using PIC-CIf you would tell what C your using hi-tech, PWC, boostC,
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
UART1_Write_Text("Start");
while (1) { // Endless loop
if (UART1_Data_Ready()) { // If data is received,
uart_rd = UART1_Read(); // read the received data,
UART1_Write(uart_rd); // and send data via UART
}
}
}
/////////////////////////////////////////////////////////////////////////
//// EX_SQW.C ////
//// ////
//// This program displays a message over the RS-232 and waits for ////
//// any keypress to continue. The program will then begin a 1khz ////
//// square wave over I/O pin B4. ////
//// ////
//// Comment out the printf's and getc to eliminate the RS232 and ////
//// just output a square wave. ////
//// ////
//// Change both delay_us to delay_ms to make the frequency 1 hz. ////
//// This will be more visable on a LED. ////
//// ////
//// ////
//// Change the device, clock and RS232 pins for your hardware if ////
//// needed. ////
/////////////////////////////////////////////////////////////////////////
#include <p30F4011.h>
#include <stdio.h>
//#device ICD=TRUE // For using the debugger, un-comment
#use delay(crystal=20mhz)
// UART1A specifies the alternate UART pins Pin_C13, Pin_C14
// use UART1 to sprcify UART for pins Pin_F3, Pin_F2
#use rs232(baud=9600, UART1A)
int main() {
printf("Press any key to beginnr");
getc();
printf("1 khz signal activatednr");
while (1) {
output_high(PIN_B4);
delay_us(500);
output_low(PIN_B4);
delay_us(500);
}
return 0;
}
#include "p30f4011.h"
#include <stdio.h>
#use rs232(baud=9600, xmit=U1TX, rcv=U1RZ) //serial port output pin & baud rate
// Set up the serial connection to use 9600 buad rate
void setupUSART1()
{
U1BRG = 11; //set baud rate to ~9600bps at 7.37MHz (FRC oscillator)
U1MODEbits.STSEL = 0; //1 stop bit
U1MODEbits.PDSEL = 0; //8-bit, no parity
U1MODEbits.UARTEN = 1; //enable UART1
U1STAbits.UTXEN = 1; //enable UART1 transmitter
U1STAbits.URXISEL = 3; //Enable 4 character
}
int main(void)
{
setupUSART1();
printf("Hello");
return 0;
}
#include "p30f4011.h"
#include <stdio.h>
void setupUSART1()
{
U1BRG = 11; //set baud rate to ~9600bps at 7.37MHz (FRC oscillator)
U1MODEbits.STSEL = 0; //1 stop bit
U1MODEbits.PDSEL = 0; //8-bit, no parity
U1MODEbits.UARTEN = 1; //enable UART1
U1STAbits.UTXEN = 1; //enable UART1 transmitter
U1STAbits.URXISEL = 3; //Enable 4 character
}
int main()
{
//Initialization
// setupADC();
// setupPWMs();
setupUSART1();
printf("Starting up...\n\r");
// delay();
TRISBbits.TRISB2 = 0; //set Port B pin 2 to output
//main loop
while (1)
{ printf("Hello");
}
return 0;
}
Got it to work on the PIC16, will probably just stick to that for now.
Any advice on reading? (as of now, I am planning to use the getc() function)