AtomSoft
Well-Known Member
Hey all I seem to be having some trouble with this dag USART on my 18f2525.
Im trying to run it at: 2400bps from 8mhz internal
I just cant get the math right for the code i think.
To calculate i did:
((FOSC/BAUD)/64)-1
so thats
((8000000/2400)/64)-1 = 51.08 = 51
To calculate actual baud from above:
8000000/(64(51+1)) = 2403
Percentage:
(2403-2400)/2400 = 0.00125%
Which i assume is great!
The code im using is:
The issue is i can get data 1 time and when i try to get more info i doesnt work.
full code
As mentioned i can read 1 card then it seems to get suck on the built in function "DataRdyUSART".
Any thoughts?
Im trying to run it at: 2400bps from 8mhz internal
I just cant get the math right for the code i think.
To calculate i did:
((FOSC/BAUD)/64)-1
so thats
((8000000/2400)/64)-1 = 51.08 = 51
To calculate actual baud from above:
8000000/(64(51+1)) = 2403
Percentage:
(2403-2400)/2400 = 0.00125%
Which i assume is great!
The code im using is:
Code:
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,51);
The issue is i can get data 1 time and when i try to get more info i doesnt work.
full code
Code:
#include <p18f2525.h>
#include <stdio.h>
#include <string.h>
#include <delays.h>
#include <usart.h>
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67, XINST = OFF
/************************************
Prototypes
*************************************/
void main(void);
void delay_ms(int mS);
void delay_us(int uS);
void delay_s(int S);
char IsNew(char TheID);
char getSlot(void);
char isDataRdyUSART(void);
void e_togg(void);
void lcd_line(char line);
void lcd_cmd(unsigned char letter);
void lcd_char(unsigned char letter);
void lcd_string(char *senpoint);
void lcd_init(void);
void delay_ms(int mS);
void delay_us(int uS);
void delay_s(int S);
void lcdByte(unsigned char dNyb,unsigned char rs);
/************************************
Variables AND Defines
*************************************/
char MyID;
char tmp;
unsigned char sPosition;
unsigned char cards[100];
unsigned char tmpID[10];
unsigned char inputID[10];
unsigned char comp[17] = " AtomSoftTech ";
unsigned char NewStr[17] = "New Card Entered";
unsigned char NonStr[17] = "Card Not Found. ";
unsigned char ExisStr[17]= "Card Exist ";
unsigned char ShowID[17] = "ID: ";
unsigned char Blank[17] = " ";
#define RX_TRIS TRISCbits.TRISC7
#define BadLED LATCbits.LATC2
#define GoodLED LATCbits.LATC3
#define AddUserBtn PORTAbits.RA1
#define AddUserBtnT TRISAbits.TRISA1
#define LCD_CLK_T TRISAbits.TRISA2//C0
#define LCD_DAT_T TRISAbits.TRISA3//C1
#define LCD_E_T TRISAbits.TRISA4//C2
#define LCD_CLK LATAbits.LATA2//C0
#define LCD_DAT LATAbits.LATA3//C1
#define LCD_E LATAbits.LATA4//C2
/************************************
Main
*************************************/
void main(void){
char inputstr[12];
char y,x;
OSCCON = 0x72; //8MHz clock
while(!OSCCONbits.IOFS); //wait for osc stable
y = x = 0;
sPosition = 0;
TRISA = TRISB = TRISC = 0;
AddUserBtnT = RX_TRIS = 1;
LCD_DAT_T = LCD_CLK_T = LCD_E_T = 0;
ADCON1 = 0x0F;
lcd_init();
delay_ms(50);
lcd_line(1);
lcd_string(comp);
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,51);
//Blank out variable
for(y=0;y<100;y++){
cards[y] = 0;
}
while(1){
loopit:
getsUSART( inputstr, 12 );
if(inputstr[0] != 0x0A) goto loopit;
if(AddUserBtn == 1){
for(y=0;y<10;y++)
inputID[y] = inputstr[y+1];
for(y=0;y<12;y++)
inputstr[y] = 0;
if(IsNew(inputID) == 1){ //Card exist
lcd_line(2);
lcd_string(ExisStr);
} else { //Card is new
x = 5;
for(y=0;y<10;y++){
ShowID[x+y] = inputID[y] ;
}
lcd_line(2);
lcd_string(ShowID);
sPosition = getSlot();
if(sPosition == 0xFF){
//No Slots Available for new data
} else {
for(x=0;x<10;x++)
cards[sPosition + x] = inputID[x];
}
delay_s(1);
lcd_line(2);
lcd_string(NewStr);
delay_s(2);
lcd_line(2);
lcd_string(Blank);
}
}
}
}
char IsNew(char TheID){
char cCount,cPos;
char x,y;
for(cCount=0;cCount<10;cCount++){
cPos = cCount * 10;
for(x=0;x<10;x++){
tmpID[x] = cards[cPos + x];
}
if(memcmp(tmpID,TheID,10) == 0)
return 1;
}
return 0;
}
char getSlot(void){
char cCount, cPos;
for(cCount=0;cCount<10;cCount++){
cPos = cCount * 10;
if(cards[cPos] == 0)
return cPos;
}
return 0xFF;
}
/***********************************
LCD Functions
************************************
Send String to LCD
************************************/
void lcd_string(char *senpoint){
delay_ms(1);
while(*senpoint != '\0'){
lcd_char(*senpoint);
senpoint++;
}
}
/************************************
Send Data(Nybble) to LCD
************************************/
void lcdByte(unsigned char dNyb,unsigned char rs){
int i;
LCD_DAT=0; //Clear 74LS164 set initial bit to 0
for(i=0;i<8;i++){ //repeat for 8 bits
LCD_CLK=1;LCD_CLK=0; //write 0's to the 164
}
for(i=0;i<4;i++){ //output the nybble
if((dNyb & 0x08) != 0)
LCD_DAT=1;
else
LCD_DAT=0;
LCD_CLK=1;LCD_CLK=0;
dNyb=dNyb<<1;
}
LCD_DAT = rs; //output the RS bit value
LCD_CLK=1;LCD_CLK=0;
LCD_DAT = 0;
LCD_CLK=1;LCD_CLK=0;
LCD_CLK=1;LCD_CLK=0;
LCD_CLK=1;LCD_CLK=0;
e_togg();
}
/************************************
Toggle E Line
************************************/
void e_togg(void){
LCD_E=1;LCD_E=0;
}
/************************************
Set LCD Line
************************************/
void lcd_line(char line){
if(line == 0x01)
lcd_cmd(0x80);
else
lcd_cmd(0xc0);
}
/************************************
LCD Send Command
************************************/
void lcd_cmd(unsigned char letter){
unsigned char temp;
temp=letter;
temp=temp>>4;
lcdByte(temp,0);
temp=letter;
temp=temp&0x0f;
lcdByte(temp,0);
}
/************************************
LCD Send Character
************************************/
void lcd_char(unsigned char letter){
unsigned char temp;
temp=letter;
temp=temp>>4;
lcdByte(temp,1);
temp=letter;
temp=temp&0x0f;
lcdByte(temp,1);
}
/************************************
LCD Initialization
************************************/
void lcd_init(void){
LCD_E_T = 0;
LCD_CLK_T = 0;
LCD_DAT_T = 0;
lcdByte(0x03,0);
delay_ms(5);
e_togg();
delay_us(160);
e_togg();
delay_us(160);
lcdByte(0x02,0);
delay_us(160);
lcd_cmd(0x28); //set 4-bit mode and 2 lines
delay_us(160);
lcd_cmd(0x10); //cursor move & shift left
delay_us(160);
lcd_cmd(0x06); //entry mode = increment
delay_us(160);
lcd_cmd(0x0d); //display on - cursor blink on
delay_us(160);
lcd_cmd(0x01); //clear display
delay_ms(500);
}
/************************************
Delays (S,mS,uS) for 20Mhz
************************************/
/*********************
Delay Second(s)
**********************/
void delay_s(int S){
int y;
char x;
for(y=0;y<S;y++)
for(x=0;x<4;x++)
delay_ms(250);
}
/************************
Delay MilliSecond(s)
*************************/
void delay_ms(int mS){
int y;
for(y=0;y<mS;y++)
Delay1KTCYx(2);
}
/************************
Delay MicroSeconds
*************************
Lowercase 'u' is common
symbol for Micro
*************************/
void delay_us(int uS){
int y;
char x;
for(y=0;y<uS;y++){
Nop();
Nop();
}
}
As mentioned i can read 1 card then it seems to get suck on the built in function "DataRdyUSART".
Any thoughts?