mariuszoll
Member level 5
- Joined
- Aug 28, 2012
- Messages
- 82
- Helped
- 1
- Reputation
- 2
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 2,147
void LCDInit(void)
{
Delay1KTCYx(240);
LATA=0;
LATC=0;
//Outputs
TRISAbits.TRISA7=0; //RS signal
TRISAbits.TRISA6=0; //EN signal
TRISCbits.TRISC0=0; // RC0 output, DB4
TRISCbits.TRISC1=0; // RC1 output, DB5
TRISCbits.TRISC2=0; // RC2 output, DB6
TRISCbits.TRISC3=0; // RC3 output, DB7
TRISCbits.TRISC5=0; // STATUS LED
TRISCbits.TRISC6=0; // UART_TX
//Inputs
TRISBbits.TRISB4=1; //data0
TRISBbits.TRISB3=1; //data1
TRISCbits.TRISC4=1; // button
TRISCbits.TRISC7=1; //UART_RX
// Oscillator frequency
OSCCON=0b01100111; // 4MHz
// pins configurred as digital
ADCON1=0b00001111;
// Interrupt config
INTCON=0x40; // PEIE high
IPR1bits.RCIP=1; // EUSART Receive Interrupt High Priority
RCONbits.IPEN=1; // enable priority levels
PIE1bits.RCIE=1; // Enable EUSART receive interrupt
LATAbits.LATA6=0; // Set En on 0
Delay1KTCYx(100); //Delay 100ms
LATAbits.LATA7=0; // Set RS on 0
Delay1KTCYx(240); //Delay 15ms
LATC=0x3;
en_set();
Delay1KTCYx(10);
LATC=0x3;
en_set();
Delay1TCYx(10);
LATC=0x3;
en_set();
Delay1TCYx(10);
LATC=0x2;
en_set();
Delay1TCYx(10);
//Function set
cmd(0x28); // 4 bits, 2 lines, 5x7 dots format display mode
Delay1TCYx(10); // delay 40us
//Display ON control
cmd(0x0E); // display on, cursor on, blinking cursor position off
Delay1TCYx(25);
//Clear Display
cmd(0x01); // dispaly clear
Delay1KTCYx(15);
//Entry set increment
cmd(0x06); // increment, shift right
Delay1KTCYx(20);
}
ADCON1 = 0b1001110;
init_value = 0x3;
// TRISA=0;
TRISD=0;
// TRISB=0;
// TRISC=0;
//
__delay_ms(200); // Powertip LCD needs a delay here to enable init
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
__delay_ms(200); // wait after power applied,
LCD_DATA = init_value;
// programme de test du COG16 LCD 2x16
// test de la librairie XLCD_lite.h
// test avec oscil Quartz 10Mhz
// usage Msg en RAM,ROM,EEPROM
//
// nota : test à 1MHz (si oscillateur interne) aussi OK..mais plus lent.
#define Versus "Ver.130806 C18 "
#define Source "18F26K20_Test_COG16_130806.c"
#define Projet "18F26K20_LCD2x16.mcp"
#define Hardware "18F26k20_COG16_LCD.txt"
#include <p18f26k20.h>
#include <../_common/LCD2x16_lite.h>
#include <stdio.h> // sprintf
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h> // pour fonctions UART
#include <adc.h>
#include <ctype.h>
#include <portb.h>
#include <timers.h>
#include <math.h>
// HARDWARE
// Q=10Mhz
// ATTENTION alim 3,6V maxi
//1 MCLR 4,K +VSS
//2 RA0 Analog input
//3 RA1 Analog input
//4 RA2
//5 RA3
//6 RA4 Output Led rouge
//7 RA5
//8 +3,6V
//9 RA6 clockIN Quartz 10Mhz 22pF
//10 RA7 clockOUT Quartz 10Mhz 22pF
//11 RC0
//12 RC1
//13 RC2
//14 RC3
//-------
//28 RB7 Data D7 LCD
//27 RB6 Data D6 LCD
//26 RB5 Data D5 LCD
//25 RB4 Data D4 LCD
//24 RB3 En LCD
//23 RB2 RS LCD
//22 RB1
//21 RB0
//20 GND 0V
//19 VSS +3,6V
//18 RC7
//17 RC6
//16 RC5
//15 RC4
//#define OSCILLATEUR_INTERNE
#ifdef OSCILLATEUR_INTERNE
#pragma config FOSC = INTIO67, FCMEN = OFF // CONFIG1H
#else
#pragma config FOSC = HS, IESO=OFF ,FCMEN=OFF, LPT1OSC=OFF
#endif
#pragma config PWRT=OFF,BOREN=OFF,WDTEN=OFF
#pragma config PBADEN=OFF,CCP2MX=PORTC
#pragma config MCLRE=ON,STVREN=OFF,LVP=OFF,XINST=OFF,DEBUG=OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF // CONFIG7H
rom const char chaine0[]="Frame Erreur et Overflow detection : ";
rom const char chaine1[]="Port comm ...... bds, ouvert \r\n";
rom const char chaine2[]="18F26K20_Test_COG16_130806.c \r\n";
rom const char chaine3[]="ESC pour sortir\r";
rom const char chaine4[]="J= ";
rom const char chaine5[]="COG16 versus C18";
rom const char chaine6[]="PIC18F26K20 3,3V";
rom const char chaine7[]="................ \r\n";
rom char *RS_Str[]={chaine0,chaine1,chaine2,chaine3,chaine4,chaine5 ,chaine6,chaine7 };
#pragma romdata EEP_=0xF00000
rom unsigned char EEPROM_data [][32] ={
// 1234567890123456789012345678901
"Proj.18F26K20_LCD2x16.mcp \r",//0 avec terminateur zero
Versus " \r", //20
"Init Hardware et Comm. \r",//40 avec terminateur zero
"Boucle en 1 Sec <ESC> exit \r",//60 avec terminateur zero
"Init BlueTooth \r",//80 avec terminateur zero
"paulfjujo@free.fr \r",//A0 sans terminateur zero
"MPLAB IDE V8.91 \r", //C0 sans terminateur zero
"C18 v3.45.00 \r", //E0
"100 \r",//100 avec terminateur zero
"120 \r",//100 avec terminateur zero
"140 \r",//100 avec terminateur zero
"160 \r",//100 avec terminateur zero
"180 \r",//100 avec terminateur zero
"1A0 \r",//100 avec terminateur zero
"1C0 \r",//100 avec terminateur zero
"1E0 \r",//100 avec terminateur zero
"200 \r",//100 avec terminateur zero
"220 \r",//100 avec terminateur zero
"240 \r",//100 avec terminateur zero
"260 \r",//100 avec terminateur zero
"280 \r",//100 avec terminateur zero
"2A0 \r",//100 avec terminateur zero
"2AC \r",//100 avec terminateur zero
"2AE \r",//100 avec terminateur zero
"300 \r",//100 avec terminateur zero
"320 \r",//100 avec terminateur zero
"340 \r",//100 avec terminateur zero
"360 \r",//100 avec terminateur zero
"380 \r",//100 avec terminateur zero
"3A0 \r",//100 avec terminateur zero
"3AC \r",
"paulfjujo@free.fr \0"
};
#pragma
#define CLS 12
#define BS 8
#define TAB 9
#define CR 13
#define LF 10
#define Bell 7
#define STX 0x02
#define ETX 0x03
#define ON 0 // logique inverse avec led tiree au +5V
#define OFF 1
#define OUT_RS232 Drapeaux.Lcd=0;
#define OUT_LCD Drapeaux.Lcd=1;
#define MAX_LEN 200
#define DureeCycle 10 // 10 x 200mS soit 2sec @10Mhz
#define Led_Rouge LATAbits.LATA4
#define Word unsigned int
#define Byte unsigned char
#pragma udata
unsigned int i,j,k;
unsigned int EAx;
float f1;
unsigned char tampon[16];
unsigned char CRam[32];
//unsigned char Drapeaux ; // 8 flags
struct chbits {
unsigned Stable:1;
unsigned standby:1;
unsigned elligible:1;
unsigned Accumule:1;
unsigned Blc:1;
unsigned Over:1;
unsigned Fill:1;
unsigned Lcd:1;
} Drapeaux;
#pragma idata
static unsigned char Entree[16]="123456789012345";
unsigned char *valtxt=&Entree[0];
static unsigned char Texte[64]="PROJET C18 :18F26K2x_LCD2x16.mcp\r";
unsigned char * txt=&Texte[0];
#define MaxCar_LCD 16.
#define LCD
//#define RS232
//--- Subroutines ================
unsigned char EEPROM_Lect(unsigned int AdrL) ; // lecture a l'adresse adrL
void EEPROM_Ecr(unsigned int AdrE,unsigned char car) ; // ecrit a l'adresse adrE
void Save_Msg_To_EEPROM(unsigned int * adrx, unsigned char * ptr1);
void PrintOut_Eeprom_Msg(unsigned int PtrE);
void Init_ADC(void);
void Init_Hardware(void);
unsigned int Mesure_ADC(int canal);
void Tempo(unsigned long val);
//------ EEPROM du PIC 18Fx6K2x 1024 bytes !---------------
unsigned char EEPROM_Lect(unsigned int Adr) // lecture a l'adresse adr
{
EEADR =(unsigned char)(Adr&0x00FF);
EEADRH =(unsigned char) ((Adr>>8)&0x0003); //Bits 2-7 are masked off since EEPROM is only 1KB
EECON1bits.EEPGD=0; // 0= adressage EEPROM
EECON1bits.CFGS=0; // 0= acces data EEPROM
EECON1bits.RD=1; // arme Lecture EEprom
Tempo(1000L);
return(EEDATA);
}
void EEPROM_Ecr(unsigned int AdrE,unsigned char car) // ecrit a l'adresse adrE
{int dr;
EEADRH = ((AdrE>>8)&0x03); //Bits 2-7 are masked off since EEPROM is only 1KB
EEADR =(AdrE &0xFF);
EEDATA=car;
EECON1bits.EEPGD=0;
EECON1bits.WREN=1; // autorise ecriture EEPROM
EECON1bits.CFGS =0;
if(INTCONbits.GIE ==1)
{
INTCONbits.GIE =0 ;
dr=1;
}
EECON2=0x55;
EECON2=0xAA;
EECON1bits.WR=1;
while (PIR2bits.EEIF==0);
PIR2bits.EEIF=0;
EECON1bits.WREN=0;
if (dr==1)INTCONbits.GIE =1;
}
void Save_Msg_To_EEPROM(unsigned int * adrx, unsigned char * ptr1)
{
while (*ptr1) EEPROM_Ecr(adrx++,*ptr1++);
}
void PrintOut_Eeprom_Msg(unsigned int PtrE)
{ int i,j;
j=0;
do
{
if ( PtrE >1023) break;
i = (int)EEPROM_Lect(PtrE);
if ( i==0) break;
#ifdef LCD
if (Drapeaux.Lcd==1) XLCDPut((Byte)i);
#endif
#ifdef RS232
if (Drapeaux.Lcd==0) Put_RS((Byte)i);
#endif
if ((Drapeaux.Lcd==1) && (j > MaxCar_LCD)) break; // limit LCD 16car
PtrE++;j++;
}
while (i!=0) ;
Tempo(1000L);
}
// Tempo(250.000); 2,2 sec avec Q=20Mhz
void Tempo(unsigned long val)
{
while(val>0){val--;}
}
unsigned int Mesure_ADC(int canal)
{ unsigned int EA;
//ADCON0bits.ADON=1;
ADCON0=1 + (canal<<2); // ADON=1 et choix de canal
Delay10TCYx( 10 ); // Delay for 100TCY
ConvertADC(); // Start conversion
while( BusyADC() );
EA = ReadADC(); // Read result
return (EA);
}
// itoa( EAx,tampon);
// Put_RS('E');Put_RS('A');Put_RS(canal+48);Put_RS('=');
// k=PutStr_RS(tampon);
// Tempo(5000L);
//}
void Init_Hardware()
{
PORTB = 0xFF;
TRISB = 0x00; // All Outputs
ANSELH=0; // no analog
INTCON2bits.RBPU=0; // enable Pull-up
WPUB=0xFF; // all pin with pull up
// ATTENTION a l'ordre des canaux ANALOGIQUES !!!
ANSEL=0;
ANSELbits.ANS0=1; // analog input on Port RA0 voir page 154
ANSELbits.ANS1=1; // analog input on Port RA1
LATA = 0x00;
TRISA = 0b00001111 ; //RA0,RA1=analog input, RA2,RA3=input, RA4,RA5=output
PORTA=0;
PORTC = 0xFF; // set PORTC to $FF
TRISC = 0b10111111; // all input expect RC6
TRISCbits.TRISC0 = 1;
TRISCbits.TRISC1 = 1;
TRISCbits.TRISC2 = 1;
TRISCbits.TRISC3 = 1;
TRISCbits.TRISC4 = 1;
TRISCbits.TRISC5 = 1;
TRISCbits.TRISC6 = 0; // TX - Set as Output UART RS232 and Bootloader
TRISCbits.TRISC7 = 1; // RX - Set Receive pin for UART RS232 and Bootloader
RCONbits.IPEN=1 ; // enable priority levels
RCONbits.SBOREN=0; // BOR disbled
SLRCON=0; // standard rate for PORTA,B,C,D,E
}
void Init_ADC()
{
//config lecture ADC 10 bits
ADCON0bits.ADON=1; // ADC enabled chanel 0
ADCON1bits.VCFG1=0; // +Vref = +5V AVdd
ADCON1bits.VCFG0=0; // -Vref = Gnd AVss
ADCON2bits.ADFM=1; // right justified
ADCON2bits.ACQT2=1;
ADCON2bits.ACQT1=1;
ADCON2bits.ACQT0=0; // 16 TAD
ADCON2bits.ADCS2=1;
ADCON2bits.ADCS1=0;
ADCON2bits.ADCS0=1; // Fosc/16
}
void main(void)
{
Init_Hardware();
Led_Rouge=ON;
Init_ADC();
Delay10KTCYx(250);
Led_Rouge=OFF;
OUT_LCD
XLCDInit();
Erase_Ligne(1);
XLCDPut('H'); //write to LCD
XLCDPut('E');
XLCDPut('L');
XLCDPut('L');
XLCDPut('L');
XLCDPut('O');
Delay10KTCYx(250);
Erase_Ligne(2);
XLCDPutRomString("COG16 vers.a C18");
Delay10KTCYx(250);
Erase_Ligne(1);
PrintOut_Eeprom_Msg(0x3E0); // fond d'Eeprom paulfjujo ..
Delay10KTCYx(250);
Delay10KTCYx(250);
txt=&Texte[0];
//strcpypgm2ram(txt,"PIC18F26K20 3,3V"); // .. OK
strcpypgm2ram(txt,RS_Str[6]); // avec texte en table rom ...ok
Erase_Ligne(1);
XLCDPutRamString(txt);
Delay10KTCYx(250);
Delay10KTCYx(250);
Erase_Ligne(1);
XLCDPutRomString("Mesure CH1"); //partie fixe
Erase_Ligne(2);
XLCDPutRomString("Mesure CH2"); //partie fixe
while(1)
{
Led_Rouge=!Led_Rouge;
EAx=Mesure_ADC(0);
txt=&Texte[0];
k=sprintf(txt,"%04d",EAx);
XLCD_pos(1,11);
XLCDPutRamString(txt);
EAx=Mesure_ADC(1);
txt=&Texte[0];
k=sprintf(txt,"%04d",EAx);
XLCD_pos(2,11);
XLCDPutRamString(txt);
Delay10KTCYx(250);
}
}
Could somebody help me with an example of ISR for an USART application?
//26-09-2013 Q=20Mhz au lieu de 10Mhe et 38400 bauds
//03/07/2012
// Pickit3
// TESTE OK en lecture
// attention: use radix decimal !!
// C18 MPLAB
// directory : _C18\MesProjets_C18\_18F46K22
// Projet :_C18\MesProjets_C18\_18F46K22\18F46K22_test_RS232.mcp
// Source : 18F26K22_test_RS232.c
// linker: 18f26k22_GrosseBank.lkr
/*
========== HARDWARE ====================
27 pf Q=10Mhz 27pF
Interface DS275 pour liaison UART TTL <-> RS232
PIC 18F26K22 28 pins DIP
PortB
RB0 21 input
RB1 22
RB2 23
RB3 24
RB4 25
RB5 26
RB6 27 ICSP_Clck Bleu fonce Pin5 Pickit2
RB7 28 ICSP_Data Bleu clair Pin4 Pickit2
PORTA
RA0 2 Analog Input for AC line meaurement
RA1 3
RA2 4
RA3 5
RA4 6 Output .. Led .. 390 ohms +5V
RA5 7 Output
RA6 8 ----- Quartz 10Mhz C=22pF -- Gnd
RA7 9 ------ Quartz --- C=22pF -- Gnd
PORTC
RC0 11
RC1 12
RC2 13
RC3 14
RC4 15 SCL I2C LCD4x20
RC5 16 SDA I2C LCD4x20 depend si DTE ou DCE
RC6 17 TX UART1 --Blc--- 3 de DS275 7 -- blanc ----> 2HE10 -nappe --DB9M DB9F cable RS232 PC
RC7 18 RX UART1 --rouge- 1 de DS275 5 ---bleu Fce -< 3HE10 -nappe --DB9M DB9F cable RS232 PC
Gnd ------------------------------------------>5 HE10 -nappe ---DB9M DB9F cable RS232 PC
1 --- jaune -- VPP/MCLR --- pin1 Pickit2
8 ---- blanc ------Gnd----- pin3 Pickit2
VSS 8 ---- GND Power
VSS 19 ---- GND Power
VDD 20 ---- +5V Power
*/
#include "p18f26k22.h"
#define p18f26k22
#define Versus "Versus 130926 C18 "
#include "stdio.h"
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h> // pour fonctions UART HARDWARE
#include <adc.h>
#include <ctype.h>
#include <portb.h>
#include <timers.h>
#include <math.h>
// configuration bits set in code !
// voir Help
// Topics
// Pic18
// config bits settings
// choisir PIC18F26K22
//#define OSCILLATEUR_INTERNE
#ifdef OSCILLATEUR_INTERNE
#pragma config FOSC = INTIO67, FCMEN = OFF, PLLCFG=OFF // CONFIG1H
#else
#pragma config FOSC = HSHP, PLLCFG=OFF, PRICLKEN=ON ,FCMEN=OFF // quartz 10Mhz
#endif
#pragma config IESO=OFF,PWRTEN=OFF,BOREN=OFF,WDTEN=OFF,CCP2MX=PORTC1
#pragma config PBADEN=OFF,T3CMX=PORTC0,P2BMX=PORTC0,CCP3MX=PORTB5
#pragma config MCLRE=EXTMCLR,STVREN=OFF,LVP=ON,XINST=OFF,DEBUG=OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF // CONFIG7H
// Using Q=20Mhz Clock
#define CLOCK_FREQ 20000000L
#define FOSC 10000000
// config vue sur Pickit3 : 2200 3C18 9900 0084 C00F E00F 400F
#ifndef Byte
#define Byte unsigned char
#endif
#define MAX_LEN 200
volatile unsigned char buffer[MAX_LEN]; // buffer de reception
#define DureeCycle 10 // * 1 diziemes de secondes => 1,0sec
// Attention use Radix decimal in MPASM options
#define CLS 12
#define BS 8
#define TAB 9
#define CR 13
#define LF 10
#define Bell 7
#define ON 0 // logique inverse avec led tiree au +5V
#define OFF 1
#define Led_Rouge PORTAbits.RA4
#define Allume 0
#define Eteint 1
//#define ToTest
#ifdef ToTest
// === variables for Testing purpose ====
//=======================
#endif
unsigned int i,j,k;
unsigned int dummy;
unsigned int j1;
unsigned int M;
unsigned int BRG_REG;
unsigned char c;
long L;
char Texte[]="18F26K22 test RS232 C18\r\n 26/09/2012 rev a\r\n38400bds Quartz 20Mhz \r\n\0";
char *txt;
volatile unsigned int i1,DataReady1;
volatile char c1;
volatile unsigned int CptErr;
volatile int count;
//--------- RS232 UART HARDWARE --------------
void Init_UART1(void);
int PutStr_RS(char *s);
void Put_RS(char * untel);
void CRLF();
void Tempo(long val);
void Octet2Hex(unsigned char number);
void Decompile_byte(Byte un);
void InterruptHandlerHigh (void);
void Decompile_byte(Byte un) {
Byte masque;
masque = 0x80;
while (masque > 0u )
{
if (un & masque)
Put_RS(49u); // '1'
else
Put_RS(48u); // '0'
masque =masque >>1;
}
}
void Octet2Hex(unsigned char number)
{
char high,low;
// high nibble
high = ((number & 0xF0) >> 4) + 48; // + '0'
if (high > '9')
high =high + 7;
// low nibble
low = (number & 0x0F) + 48; // +'0' low nibble
if (low > '9') // '9'= 57u
low += 7; // > '9'
Put_RS(high);
Put_RS(low);
Put_RS('h');
}
//gestionnaire d'interruption
//------------------------------
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
_asm
goto InterruptHandlerHigh //jump to interrupt routine
_endasm
}
// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh ()
{
// static unsigned int i1 ; // doit être statique pour conserver sa valeur entre les IT
//======== SERIAL======================
if(PIR1bits.RC1IF==1) // si une interruption arrive
{
if(RCSTA1bits.FERR==1 )
{
RCSTA1bits.SPEN = 0 ;
CptErr++;
RCSTA1bits.SPEN= 1 ;
}
if (RCSTA1bits.OERR==1) // voir parag 16.1.26 p273
{
RCSTA1bits.CREN = 0 ;
RCSTA1bits.CREN = 1 ;
CptErr++;
}
c1 =Read1USART(); // le lire => RAZ RCIF
// for echoed car
//while(Busy1USART()); // par sécurité
//if (PORTBbits.RB1==0) Write1USART(c1); // echo
if( c1 == BS)
{
if(i1>0) i1--;
}
else
{
if((c1 != CR) && (i1<MAX_LEN))
{
buffer[i1]=c1 ; // stockage
i1++;
}else
{
buffer[i1]='\0'; // fin de chaîne si CR
i1=0;
DataReady1 =1;
}
}
}
} // fin de routine interrupts
void Init_UART1(void)
{
// USART_BRGH_HIGH,32
// init 38400Bd a 20Mhz
// init 19200Bd a 10Mhz avec IT en rx
// voir para 18.5 spec sheet DS41159B page 186
Open1USART( USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,32
);
INTCONbits.PEIE = 0 ; // autorisation des IT des périphériques
INTCONbits.GIE = 0 ; // autorisation globale des IT
}
int PutStr_RS(char *s)
{
int n;
for (n = 0; *s; n++, s++)
{
Put_RS(*s);
}
return n;
}
void Put_RS(char * untel)
{
while(Busy1USART());
Write1USART(untel);
}
void CRLF()
{
Put_RS(CR); Put_RS(LF);
}
void Tempo(long val)
{
while(val>0){val--;}
}
void main()
{
#ifndef ToTest
OSCCON=0;
#ifdef OSCILLATEUR_INTERNE
OSCCONbits.SCS=0x10; // see DS page 37
OSCCONbits.IRCF = 7; // choix 16Mhz
/*
OSCCONbits.IRCF = 6; // choix 8Mhz
OSCCONbits.IRCF = 5; // choix 4Mhz
OSCCONbits.IRCF = 4; // choix 2Mhz
OSCCONbits.IRCF = 3; // choix 1Mhz par defaut !!!
*/
OSCCON2=0;
OSCTUNEbits.PLLEN = 1; // 1=active PLL => 10x4=40Mhz
OSCTUNEbits.TUN=0x000000;
#endif
//OSCCON =0b01100000;
ANSELA=0;
TRISA = 0b00001111 ;
TRISAbits.TRISA4 = 0; // led rouge
LATA = 0x00;
ANSELC=0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 1;
Led_Rouge=Allume;
Init_UART1();
Put_RS('*');
CRLF();
Put_RS('#');
CRLF();
Put_RS(CLS); // clear Vbray terminal Screen
Tempo(1000000L);
txt=&Texte[0];
k=PutStr_RS(txt);
CRLF();
Tempo(20000);
// write to the USART
//k=fprintf(_H_USART,"Init UART 19200bds Q=10Mhz\r\n");
k=fprintf(_H_USART,"Init UART 38400bds Q=20Mhz\r\n");
k=fprintf(_H_USART,"hello\r\n");
k=fprintf(_H_USART,"Type some chars and CR to send them: ");
buffer[0]=0;
DataReady1=0;
i1=0;
c1=0;
CptErr=0;
// init pour interruptions ----
INTCONbits.GIE = 1; // active global interrupt
INTCONbits.GIEL=1;
RCSTA1bits.CREN= 1 ;
PIE1bits.RC1IE = 1;
INTCONbits.PEIE = 1; // autorisation des IT des périphériques
#endif
//=========Zone for short tests===================
while(1)
{
if (DataReady1==1) // test if reception occured
{
j=0;
Led_Rouge=Allume;
k= fprintf(_H_USART,"\r\nKeyboard entry : ");
while (buffer[j]!=0)
{
c=buffer[j];
Put_RS(c);
j++;
}
DataReady1=0;
buffer[0]=0;
i1=0;
Led_Rouge=Eteint;
}
if (CptErr>0)
{
k=fprintf(_H_USART,"\nNombre erreur %5d\n",CptErr);
CptErr=0;
}
//Led_Rouge=!Led_Rouge;
}
}
/* results on Vbray terminal
18F26K22 test RS232 C18
03/07/2012 rev a
38400bds Quartz 20Mhz
Init UART 38400bds Q=20Mhz
hello
Type some chars and CR to send them:
Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
17:50:14.656> Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
17:50:14.906> Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
17:50:15.093> Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
17:50:16.906> Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
17:50:17.156> Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
17:50:17.406> Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
Keyboard entry : 12345678901234567890123456789012345678901234567890123456789A123456789012345678901234567890B
*/
/*
program ScriptTest;
const STRING1 =
'12345678901234567890123456789012345678901234567890123456789A123456789012456789Z';
var i,j: longint;
begin
j:=0;
for i:=0 to 8 do
begin
comsendstr(STRING1);comsendchr(13);
Delay(20);
end;
end;
end.
*/
Here is my project file attached.
I made a test app, where I read char-by-char from Hyperterminal, and then I display on the LCD. In my opinion, it works as expected.
lcd_goto() routine works.
Please find below the code:
while(1)
{
if(PIR1bits.RCIF==1)
{
rxchar=ReadUSART();
TXREG=rxchar;
PIR1bits.RCIF=0;
[B][COLOR="#0000CD"] // test if overlap the array[/COLOR][/B]
if((rxchar!='\r') && (r<31))
{
rxarray[r]=rxchar;
string[r]=rxarray[r];
r++;
col++;
flag=0;
}
if(rxchar=='\r')
{
m=r-1;
r=0;
col=0;
row=1;
flag=1;
for(i=0; i<strlen(rxarray); i++) rxarray[i]='\0';
TXREG=12;
cmd(0x01);
for(i=0; i<=m; i++)
{
col=i;
if(col<16 && row==1)
{
lcd_goto(0+col);
}
if(col>=16 && row==1)
{
col=0;
row=2;
lcd_goto(20+col);
}
send_char(string[i]);
Delay10KTCYx(3);
col++;
} // for
}// if(rxchar=='\r')
} //if PIR
}// while(1)
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 #include <stdio.h> #include <string.h> #define LINE_LEN 17 // 16 + ending 0. Adapt the size to your LCD char lcd_line[LINE_LEN] ; void send_dec_lcd(unsigned long data) { sprintf(lcd_line, "%ld", data); lcd_string(lcd_line); }
Code C - [expand] 1 2 3 4 5 6 7 void lcd_string(const char *s) { uint8 i; while((s[i] != 0) && (i < LINE_LEN)) { send_char(s[i]); i++; } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 sd_status = SDInit(); if(sd_status == 0) { // Do something } else if(sd_status == 1) { // Do something else } else { // Do another thing. }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 sd_status = SDInit(); if(sd_status == SD_OK) { // Do something } else if(sd_status == SD_PROTECTED) { // Do something else } else { // sd_status == SD_MISSING // Do another thing. }
Hello!
I have had a look at your code.
Apparently, since you use <string.h>, you can probably also use <stdio.h>, right?
In this case, why not using existing libraries.
Example: you write a send_dec_lcd function that does nothing more than printf, and on top of that,
it does not perform it nicely. Here is an alternative (not tested, I don't use PIC).
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 #include <stdio.h> #include <string.h> #define LINE_LEN 17 // 16 + ending 0. Adapt the size to your LCD char lcd_line[LINE_LEN] ; void send_dec_lcd(unsigned long data) { sprintf(lcd_line, "%ld", data); lcd_string(lcd_line); }
Dora.
#include <p18f2620.h>
#include <delays.h>
#include <string.h>
#include <usart.h>
// Pragma
#pragma config WDT = OFF // watch dog disabled
#pragma config OSC = INTIO67
#pragma config IESO = OFF //Oscillator Switchover mode disabled
#pragma config XINST = OFF
#pragma config BOREN = OFF //Brown-out Reset disabled in hardware and software
#pragma config PBADEN=OFF //PORTB<4:0> pins are configured as digital I/O on Reset
#pragma config STVREN=OFF //Stack full/underflow will not cause Reset
#pragma config LVP=OFF //Single-Supply ICSP disabled
#pragma config MCLRE=ON //MCLR pin enabled
//*****************************************************************************
// CONSTANT DEFINITION
//*****************************************************************************
# define databits LATC // LCD 4 bit data PORT
# define data0 PORTBbits.RB4 // data0(green wire) of RFID tag
# define data1 PORTBbits.RB3 // data1(white wire) of RFID tag
# define btn1 PORTCbits.RC4 // button for save mode (active low)
//*****************************************************************************
// Variables
char txarray[9]="";
char rxarray[23]="";
char string[23]="";
int i=0,q,m=0;
unsigned char rxchar="",r=0,flag=0, icount=0,check=0,col,row;
/** L O C A L F U N C T I O N S *******************************************/
void InterruptHandlerHigh (void);
void set_hi_nibble(unsigned char data) // output hi nibble data on port lines
{
unsigned char temp=LATC;
temp= temp & 0xF0;
temp= temp | (data>>4);
LATC=temp;
}
void set_lo_nibble(unsigned char data) // output low nibble data on port lines
{
unsigned char temp=LATC;
temp= temp & 0xF0;
temp= temp | (data & 0x0F);
LATC=temp;
}
void en_set(void) //function used to set Enable signal properly
{
LATAbits.LATA6 = 1; // Set EN on 1
Delay1KTCYx(1);
LATAbits.LATA6 = 0; // Set EN on 0
}
void send_char(unsigned char c)
{
LATAbits.LATA7=1; // set RS on 1
set_hi_nibble(c);
en_set();
Delay1KTCYx(5); // delay 50ms
set_lo_nibble(c);
en_set();
Delay1KTCYx(5); // delay 5ms
}
void cmd(unsigned char c)
{
LATAbits.LATA7=0; // set RS on 0
set_hi_nibble(c);
en_set();
Delay1KTCYx(5); // delay 50ms
set_lo_nibble(c);
en_set();
Delay1KTCYx(5); // delay 5ms
}
void InitUART(void)
{
TXSTAbits.TX9=0;
TXSTAbits.TXEN=1;
TXSTAbits.SYNC=0;
TXSTAbits.BRGH=1;
TXSTAbits.TX9D=0;
RCSTAbits.SPEN=1;
BAUDCONbits.BRG16=0;
SPBRG=25; // 9600KBaud
// receive config
RCSTAbits.RX9=0;
RCSTAbits.CREN=1;
RCSTAbits.RX9D=0;
}
void LCDInit(void)
{
Delay10KTCYx(20); //Delay 200ms
LATA=0;
LATC=0;
//Outputs
TRISAbits.TRISA7=0; //RS signal
TRISAbits.TRISA6=0; //EN signal
TRISCbits.TRISC0=0; // RC0 output, DB4
TRISCbits.TRISC1=0; // RC1 output, DB5
TRISCbits.TRISC2=0; // RC2 output, DB6
TRISCbits.TRISC3=0; // RC3 output, DB7
TRISCbits.TRISC5=0; // STATUS LED
TRISCbits.TRISC6=0; // UART_TX
//Inputs
TRISBbits.TRISB4=1; //data0
TRISBbits.TRISB3=1; //data1
TRISCbits.TRISC4=1; // button
TRISCbits.TRISC7=1; //UART_RX
// Oscillator frequency
OSCCON=0b01100111; // 4MHz
// pins configurred as digital
ADCON1=0b00001111;
// Interrupt config
INTCONbits.GIE=1;
INTCONbits.GIEH=1; // enables all high priority interrupts
IPR1bits.RCIP=1; // EUSART Receive Interrupt High Priority
RCONbits.IPEN=1; // enable priority levels
PIE1bits.RCIE=1; // Enable EUSART receive interrupt
INTCONbits.PEIE=1;
LATAbits.LATA6=0; // Set En on 0
Delay1KTCYx(100); //Delay 100ms
LATAbits.LATA7=0; // Set RS on 0
Delay1KTCYx(15); //Delay 15ms
LATC=0x3;
en_set();
Delay1KTCYx(5); // delay 5ms
LATC=0x3;
en_set();
Delay100TCYx(1); // delay 100us
LATC=0x3;
en_set();
Delay10TCYx(4); // delay 40us
LATC=0x2;
en_set();
Delay10TCYx(4); // delay 40us
//Function set
cmd(0x28); // 4 bits, 2 lines, 5x7 dots format display mode
Delay10TCYx(4); // delay 40us
//Display ON control
cmd(0x0E); // display on, cursor on, blinking cursor position off
Delay10TCYx(4); // delay 40us
//Clear Display
cmd(0x01); // dispaly clear
Delay1KTCYx(2); // delay 2ms
//Entry set increment
cmd(0x06); // increment, shift right
Delay100TCYx(10); // delay 1ms
}
void putch(unsigned char byte)
{
while(!TXSTAbits.TRMT);
TXREG=byte;
}
void send_dec_usart(void) //convert binary number and display number in decimal
{
txarray[8]='\r';
q=0;
while(q<=8)
{
putch(txarray[q]);
q++;
}
}
void lcd_goto(unsigned char data) //set the location of the lcd cursor
{
if(data<16) //if the given value is (0-15) the
{ //cursor will be at the upper line
cmd(0x80+data);
}
else //if the given value is (20-35) the
{ //cursor will be at the lower line
data=data-20; //location of the lcd cursor(2X16):
cmd(0xC0+data); // -----------------------------------------------------
} // | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| |
} // | |20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| |
// -----------------------------------------------------
void lcd_string(const char *s)
{
while(*s)
{
// Delay1KTCYx(10); //Delay 10ms
send_char(*s++);
}
}
void writeString(const char *s)
{
while(*s)
{
while(!TXSTAbits.TRMT);
TXREG=*s;
s++;
}
}
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
_asm
goto InterruptHandlerHigh //jump to interrupt routine
_endasm
}
// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh()
{
if(PIR1bits.RCIF)
{
if(RCSTAbits.FERR==1) // frame eror
{
// rxchar=ReadUSART();
RCSTAbits.CREN=0;
RCSTAbits.CREN=1;
}
if(RCSTAbits.OERR==1) // overrun error
{
RCSTAbits.CREN=0;
RCSTAbits.CREN=1;
}
rxchar=ReadUSART();
// TXREG=rxchar;
PIR1bits.RCIF=0;
if(rxchar!='\r')
{
rxarray[r]=rxchar;
string[r]=rxarray[r];
r++;
col++;
flag=0;
}
if(rxchar=='\r')
{
m=r-1;
r=0;
col=0;
row=1;
flag=1;
for(i=0; i<strlen(rxarray); i++) rxarray[i]='\0';
// TXREG=12;
cmd(0x01);
}
}
}
///////////////////functia main///////////////////////////////////////////
void main()
{
//Initializations
PORTC=0x00;
LCDInit();
InitUART();
r=0;
col=0;
row=1;
flag=0;
// TXREG=12;
while(1)
{
if(flag==1)
{
for(i=0; i<=m; i++)
{
col=i;
if(col<16 && row==1)
{
lcd_goto(0+col);
}
if(col>=16 && row==1)
{
col=0;
row=2;
lcd_goto(20+col);
}
send_char(string[i]);
Delay10KTCYx(3);
}
flag=0;
}
}// while(1)
}//main
Code C - [expand] 1 rxchar=ReadUSART();
Code C - [expand] 1 rxchar = RCREG;
when "time is up" messaged is sent approximately in the same time with the "ok"
send_char(string[i]);
Delay10KTCYx(3);
....
LATCbits.LATC4=0; // led FERR off
LATCbits.LATC5=0; // led OERR oof
while(1)
{
......
if(RCSTAbits.FERR==1) // frame eror
{
// rxchar=ReadUSART();
RCSTAbits.CREN=0;
RCSTAbits.CREN=1;
LATCbits.LATC4=1;
}
if(RCSTAbits.OERR==1) // overrun error
{
RCSTAbits.CREN=0;
RCSTAbits.CREN=1;
LATCbits.LATC5=1;
}
char string1[5]="ok";
char string2[15]="time is up";
void display_ok(void)
{
for(k=0; k<strlen(string1); k++)
{ WriteUSART(string1[k]);
while(!TXSTAbits.TRMT);
}
TXREG='\r';
}
void display_nok_time(void)
{
for(k=0; k<strlen(string2); k++)
{ WriteUSART(string2[k]);
while(!TXSTAbits.TRMT);
}
TXREG='\r';
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 unsigned char *msg[2]= {"ok", "time is up"}; void UART_Write(unsigned char uartData){ TXREG = uartData; while(!TXSTAbits.TRMT); } void UART_Write_Text(unsigned char msgIdx) { while(*msg[msgIdx]) UART_Write(*msg++); UART_Write(0x0D); UART_Write(0x0A); } UART_Write_Text(0); //Prints "ok" UART_Write_Text(1); //Prints "time is up"
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 void InterruptHandlerHigh(void); //---------------------------------------------------------------------------- // High priority interrupt vector #pragma code InterruptVectorHigh = 0x08 void InterruptVectorHigh (void) { _asm goto InterruptHandlerHigh //jump to interrupt routine _endasm } //---------------------------------------------------------------------------- // High priority interrupt routine #pragma code #pragma interrupt InterruptHandlerHigh void InterruptHandlerHigh(){ }
send_char(string[i]);
Delay10KTCYx(3); <- cancel this
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?