Eric_O
Advanced Member level 4
Here under a former code obtained here. I insert a new routine for conversion of long to char ... but minus never display on LCD while counting negatives values ...
Some body have an idea ?
Merci !
Here under my code ...
Some body have an idea ?
Merci !
Here under my code ...
C:
// LCD module connections :
// VSS (pin 1) -> GND
// VDD (pin 2) -> VCC
// VEE (pin 3) -> middle pin contrast potentiometer
sbit LCD_RS at RB4_bit; // RS (pin 4) -> portB.b4 (pin 37)
// RW not used. // RW (pin 5) -> GND
sbit LCD_EN at RB5_bit; // EN (pin 6) -> portB.b5 (pin 38)
// D0 (pin 7) -> GND
// D1 (pin 8) -> GND
// D2 (pin 9) -> GND
// D3 (pin 10) -> GND
sbit LCD_D4 at RB0_bit; // D4 (pin 11) -> portB.b0 (pin 33)
sbit LCD_D5 at RB1_bit; // D5 (pin 12) -> portB.b1 (pin 34)
sbit LCD_D6 at RB2_bit; // D6 (pin 13) -> portB.b2 (pin 35)
sbit LCD_D7 at RB3_bit; // D7 (pin 14) -> portB.b3 (pin 36)
// (pin 15) -> GND
// (pin 16) -> GND
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
/******************************************************************************/
// Declaration of variables :
float sec; // - 1.5 x 10 puissance 45 to + 3.4 x 10 puissance 38
//double sec; // - 1.5 x 10 puissance 45 to + 3.4 x 10 puissance 38
//long sec;
char texte[64]; // 0 to 255
// Pour Essais 16.3, 16.4 :
// Variables ci-dessous sorties du main() pour les déclarer en global.
// Le PIC 16F a moins de mémoire RAM et ROM que le PIC 18F.
unsigned char *pointeur_de_char; // Déclaration d'un pointeur (*) de char "pointeur_de_char".
//char texte[] = "Counting ..."; // Déclaration d'un tableau "texte" de type char et initialisé avec la chaine de caractères "Counting ..." :
// En n'indiquant aucun chiffre entre les cochets [], le compilateur ajustera automatiquement la taille du
// tableau, et allouera en mémoire RAM, le bon nombre d'éléménts, soit 13 ...
char txt[13];
// Routines :
void LCD_E_pulse(void)
{
LCD_EN = 1;
Delay_us(5);
LCD_EN = 0;
}
void LCD_Write_Cmd(unsigned char octet)
LCD_RS = 0; // Select Command Register.
// Send upper nibble.
LCD_D4 = ((octet & 0x10) == 0x10) ? 1 : 0; // Si ((octet & 0x10) == 0x10) alors LCD_D4 = 1 sinon = 0.
LCD_D5 = ((octet & 0x20) == 0x20) ? 1 : 0; // Si ((octet & 0x20) == 0x20) alors LCD_D5 = 1 sinon = 0.
LCD_D6 = ((octet & 0x40) == 0x40) ? 1 : 0; // Si ((octet & 0x40) == 0x40) alors LCD_D6 = 1 sinon = 0.
LCD_D7 = ((octet & 0x80) == 0x80) ? 1 : 0; // Si ((octet & 0x80) == 0x80) alors LCD_D7 = 1 sinon = 0.
LCD_E_Pulse();
// Send lower nibble.
LCD_D4 = ((octet & 0x01) == 0x01) ? 1 : 0; // Si ((octet & 0x01) == 0x01) alors LCD_D4 = 1 sinon = 0.
LCD_D5 = ((octet & 0x02) == 0x02) ? 1 : 0; // Si ((octet & 0x02) == 0x02) alors LCD_D5 = 1 sinon = 0.
LCD_D6 = ((octet & 0x04) == 0x04) ? 1 : 0; // Si ((octet & 0x04) == 0x04) alors LCD_D6 = 1 sinon = 0.
LCD_D7 = ((octet & 0x08) == 0x08) ? 1 : 0; // Si ((octet & 0x08) == 0x08) alors LCD_D7 = 1 sinon = 0.
void LCD_Write_Data(char octet)
LCD_RS = 1; // Select Data Register.
void strConstRamCpy(unsigned char *dest, const code char *source)
while (*source)*dest ++ = *source ++;
*dest = 0; // Terminateur "0" fin de chaine de caractère.
void LCD_Write_CString_v2(char *msg)
int k;
k = 0;
//while(*(msg) > 0) // FAIL.
while(*(msg + k) > 0)
LCD_Write_Data(*(msg + k)); // Data pointée par (msg + k).
k++;
if (k > 15) break; // Si k > 15 sortie de la boucle while ...
void LCD_CString_Position_v2(char row, char col, char *msg)
if (row == 1)
{
LCD_Write_Cmd((col & 0x0F)|0x80); // Print message on 1st row and desired location.
}
else if (row == 2)
LCD_Write_Cmd((col & 0x0F)|0xC0); // Print message on 2nd row and desired location.
LCD_Write_CString_v2(msg); // OK.
void print_float_v7(char *flt, long number, char digits)
if (number < 0)
number = - number;
*(flt) = 45; // 45 = '-'
*(flt + 1) = 32; // 32 = space
if (number >= 0)
*(flt) = 32; // 32 = space
if (digits == 2)
*(flt + 2) = number / 1000000 + 48;
//if (*(flt + 2) == 48) *(flt + 2) = 32; // 32 = space
*(flt + 3) = ((number % 1000000) / 100000) + 48;
*(flt + 4) = ((number % 100000) / 10000) + 48;
*(flt + 5) = ((number % 10000) / 1000) + 48;
*(flt + 6) = ((number % 1000) / 100) + 48;
*(flt + 7) = ((number % 100) / 10) + 48;
*(flt + 8) = ((number % 10) / 1) + 48;
*(flt + 9) = 44; // 44 = ','
*(flt + 10) = ((number % 100) / 10) + 48;
*(flt + 11) = (number % 10) + 48;
*(flt + 12) = 0;
else
{
*(flt + 2) = number / 100000 + 48;
//if (*(flt + 2) == 48) *(flt + 2) = 32; // 32 = space
*(flt + 3) = ((number % 100000) / 10000) + 48;
*(flt + 4) = ((number % 10000) / 1000) + 48;
*(flt + 5) = ((number % 1000) / 100) + 48;
*(flt + 6) = ((number % 100) / 10) + 48;
*(flt + 7) = 44; // 44 = ','
*(flt + 8) = (number % 10) + 48;
*(flt + 9) = 0;
}
void LCD_Init_v4 (void)
Delay_ms(15); // LCD power ON initialization time >= 15 mS.
LCD_Write_Cmd(0x30); // 4 datas bits > Initialization of LCD with nibble method (4 datas bits).
LCD_Write_Cmd(0x02); // 4 datas bits > Initialization of LCD with nibble method (4 datas bits).
LCD_Write_Cmd(0x28); // 4 datas bits > 2 lines display, 5 × 8 dot character font.
LCD_Write_Cmd(0x0C); // 4 datas bits > Display ON. Cursor OFF.
LCD_Write_Cmd(0x06); // 4 datas bits > Auto increment cursor.
LCD_Write_Cmd(0x01); // 4 datas bits > Clear display.
Delay_ms(1); // Ajustable ... (indispensable, sinon affichage erratique à la mise sous tension)
// Main program :
void main()
ANSEL = 0; // Configure AN pins as digital I/O.
ANSELH = 0;
C1ON_bit = 0; // Disable Comparator 1.
C2ON_bit = 0; // Disable Comparator 2.
PORTB = 0;
TRISB = 0;
PORTC = 0;
TRISC = 0;
// Essai 18.1 : OK
Lcd_Init_v4(); // Initialize LCD.
while(1) // Endless loop.
unsigned char *pointeur_de_char; // Déclaration d'un pointeur (*) de char "pointeur_de_char".
//char texte[] = "Counting ..."; // Déclaration d'un tableau "texte" de type char et initialisé avec la chaine de caractères "Counting ..." :
// En n'indiquant aucun chiffre entre les cochets [], le compilateur ajustera automatiquement la taille du
// tableau, et allouera en mémoire RAM, le bon nombre d'éléménts, soit 13 ...
char txt[13];
pointeur_de_char = &texte[0]; // pointeur_de_char pointe sur le premier élément du tableau "texte", soit texte[0].
// Autrement dit, pointeur_de_char contient l'adresse (&) de texte[0].
//for(sec = -1000000; sec <= 1000000; sec = sec + 0.5) // sec déclarée en float. OK.
//for(sec = -8000000; sec <= 8000000; sec = sec + 0.5) // sec déclarée en float. OK.
for(sec = -8388607; sec <= 8388607; sec = sec + 0.5) // sec déclarée en float. OK. Plage maximale : 8388607 = (2 puissance 23) - 1.
//for(sec = -8388608; sec <= 8388608; sec = sec + 0.5) // sec déclarée en float. FAIL. 8388608 = 2 puissance 23.
//for(sec = -9000000; sec <= 9000000; sec = sec + 0.5) // sec déclarée en float. FAIL.
{
portc.b2 = 1; // LED yellow ON.
strConstRamCpy(pointeur_de_char, "Counting ..."); // OK.
LCD_CString_Position_v2(1, 0, pointeur_de_char); // OK. Voir dans void LCD_Write_CString_v2(char *msg), le while(*(msg + k) > 0).
//LCD_CString_Position_v2(1, 0, "Counting ...");
//print_float(sec, txt, 8); // On LCD : Line 1 : Decounting ... Line 2 : nothing
//print_float_v2(sec, txt, 8); // On LCD : Line 1 : Decounting ... Line 2 : nothing
//print_float_v3(sec, txt, 8); // On LCD : Line 1 : Decounting ... Line 2 : unreadable 16 characters
//print_float_v4(sec, txt, 8); // On LCD : Line 1 : Decounting ... Line 2 : unreadable 16 characters
//print_float_v5(&txt[0], sec, 2); // On LCD : Line 1 : Decounting ... Line 2 : Count from 00.00 to 99.99.
//print_float_v6(&txt[0], sec, 2); //
print_float_v7(&txt[0], sec, 2);
//LCD_CString_Position_v2(2, 0, txt); // Write txt in 2nd row, starting at 1st digit.
LCD_CString_Position_v2(2, 0, &txt[0]);
LCD_CString_Position_v2(2, 13, "uS"); // Write "uS" in 2nd row, starting at 14th digit
Delay_us(1);
}
portc.b2 = 0; // LED yellow OFF.
portc.b3 = 1; // LED green ON.
portc.b4 = 1; // BUZZER ON.
Delay_ms(5000);
portc.b3 = 0; // LED green OFF.
portc.b4 = 0; // BUZZER OFF.
}
Last edited by a moderator: