View previous topic :: View next topic |
Author |
Message |
Knight_FlyCn
Joined: 10 May 2013 Posts: 16 Location: Việt Nam
|
uart pic 18F4620 |
Posted: Sat May 18, 2013 2:14 am |
|
|
I want function send data to port TX of Uart. I used pic 18f4620. who can help me.
Code: | /* _CPUDIV_OSC1_PLL2_1L, // Divide clock by 2
_FOSC_HS_1H, // Select High Speed (HS) oscillator
_WDT_OFF_2H, // Watchdog Timer off
MCLRE_ON_3H // Master Clear on
*/
#include <RFD.h>
#include <stdlib.h>
#include "def_18f4620.c"
#include "slave_1_tx_rx.h"
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
///////////////////////////////////////////////////////////////////////////
#ifndef FREQ
#define FREQ 4000000 /// 4Mhz
#define baud 9600
#define spbrg_value (((FREQ/16)/baud)-1)
////////////////////////////////////////////////////////
int StartFlag = 0;
int StopFlag = 0;
char array[12];
char i = 0;
///////////////////////////////////////////////////////
#bit CS = 0xF80.4
#bit RST = 0xF82.2
#bit INT_ = 0xF81.0
#bit WAKE = 0xF82.1
#bit CS_Direction = 0xF92.4
#bit RST_Direction = 0xF94.2
#bit INT_Direction = 0xF93.0
#bit WAKE_Direction = 0xF94.1
///////////////////////////////////////////////////////////////////////////////
// Command: 0x00: IP request //
// 0x01: I_AM_LM35 //
// 0x02: I_AM_PIR
INT TEMP;
int adress;
INT VALUE;
INT16 k=0;
const int IP_request=0x00,adress_surport=0x01,ip_surport=0x10;
int I_AM_LM35=0x01;
INT DATA_SEND=0X02;
INT COMMAND;//
INT1 IP_REQUEST_FLAG=FALSE;
SHORT SLEEP_MODE=true;
//!#INT_RTCC
//!VOID NGAT_TIMER0()
//!{ printf("ngat timer0!");
//! k++;
//! IF (k>= 1000)
//! {
//! k=0;
//! SLEEP_MODE=FALSE;
//! }
//! TMR0IF=0;
//! disable_interrupts(INT_TIMER0);
//!}
//!
#INT_EXT
void ngat_RB0()
{
temp = read_ZIGBEE_short(INTSTAT); // Read and flush register INTSTAT
if ((temp&(1<<3))==(1<<3))
{
read_RX_FIFO();
COMMAND =DATA_RX[0];
SWITCH (COMMAND)
{
CASE ip_surport:
ADRESS=DATA_RX[1];
rfd_ADDRESS_short[1]=adress;
IP_REQUEST_FLAG=TRUE;
BREAK;
break;
}
}
}
///////////////////////////////////////////////////////////////////////////////
void uart_init(void)
{
SPEN =1;
SYNC = 1;
BRGH = 1;
SPBRG = spbrg_value;
CREN =1;
GIE =1;
PEIE =1;
RCIE =1;
}
void interrupt_RXR()
{
RCIF = 0;
if(RCREG == 0X02)
{
StartFlag = 1;
}
if(StartFlag == 1 && RCREG != 0X03)
{
array[i] = RCREG;
i++;
}
if(RCREG == 0X03)
{
array[i] = '\0';
StopFlag = 1;
i = 0;
}
}
///////////////////////////////////////////////////////////////////////////////
void main()
{
/////////////////////////////////////////////////////////////
TRISC0=TRISD3=0;
TRISC7=1;
TRISC6=0;
////////////////////////////////////////////////////////////
CS_Direction = 0; // Set direction to be output
RST_Direction = 0; // Set direction to be output
INT_Direction = 1; // Set direction to be input
WAKE_Direction = 0; // Set direction to be output
Delay_ms(5);
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4); // Initialize SPI module
pin_reset(); // Activate reset from pin
software_reset(); // Activate software reset
RF_reset(); // RF reset
set_WAKE_from_pin(); // Set wake from pin
set_long_address(ADDRESS_long_1);
set_short_address(rfd_ADDRESS_short); // Set short address
set_PAN_ID(PAN_ID); // Set PAN_ID
init_ZIGBEE_nonbeacon(); // Initialize ZigBee module
nonbeacon_device();
set_TX_power(31); // Set max TX power
set_frame_format_filter(1); // 1 all frames, 3 data frame only
set_reception_mode(1); // 1 normal mode
enable_int();
pin_wake(); // Wake from pin
uart_init();
while(1)
{
if(StopFlag == 1)
{
StopFlag == 0;
StartFlag == 0;
for(i=0; i <=13; i++)
{
array[i]=0;
}
i=0;
}
}
}
|
Now i want send data in array to TX. So send to PC. I want function " uart_write" _________________ Thinh |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19589
|
|
Posted: Sat May 18, 2013 4:00 am |
|
|
putc()
To format the data printf()
Get rid of your uart_int function. This is already done for you by #use RS232.
Look at ex_sisr.c for how to handle a receive interrupt. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Sat May 18, 2013 5:19 am |
|
|
also
always add 'errors' to the use rs232(...options...) when using the hardware UART to avoid it from 'locking up' due to framing errors.
and..
code like 'temp&(1<<3))==(1<<3) is 'messy'.
you should 'play computer', see what 1<<3 is and use that number instead. It saves processor time trying to figure out what it is TWICE and you'll be able to debug problems better.In fact if you define a variable say 'errorbit' or whatever it really is, then tomorrow or next week you'll KNOW what is going on.
hth
jay |
|
|
|