|
|
View previous topic :: View next topic |
Author |
Message |
rlabee
Joined: 25 Jan 2023 Posts: 7
|
I2C stuck on first task (pic16f877a) |
Posted: Wed Jan 25, 2023 10:09 am |
|
|
Hi all,
I've been browsing this forum for a while now, but never made an account. Although I've learned a lot from all of you guys I'm stuck on my project.
The assignment is to make a miniature elevator that works and shows messages regarding the elevator on a 2x16 LCD.
My problem here is that the elevator works (kind off, still under construction) and the LCD was showing the correct information. Until i had a power loss. Now the whole program seems to be stuck at the very beginning.
I've tried programming everything on another 16F877A chip but it still has the same problems.
I also had 4.7k pull-up resistors between the SDA and SCL lines.
I hope somebody can help me with this problem, I'm stuck on it for days now.
I would also like to mention that every tip or trick anyone can give about programming or making my program easier is more than welcome.
Down below my code. Thanks in advance!
(Excuse my English, I'm Dutch)
Code: | #include <16f877a.h>
#include <STDIO.h>
#fuses HS,NOWDT
#use delay(clock=20000000)
#use I2C(MASTER, SCL=PIN_C3, SDA=PIN_C4, SLOW)
int16 a, b, c, d, i, r, v, x;
char j;
unsigned char address[1] = {0x4E}; //adres van de 2x16 segment display
unsigned char LCD = 0;
const unsigned char begane_grond[32] = {"Begane grond "}; // 0x00
const unsigned char eerste_ver[32] = {"Verdieping 1 "}; // 0x01
const unsigned char tweede_ver[32] = {"Verdieping 2 "}; // 0x02
const unsigned char derde_ver[32] = {"Verdieping 3 "}; // 0x03
const unsigned char begin[32] = {" Lift start op "}; // 0x04
const unsigned char Naarbeganegrond[32] = {"Lift gaat naar Begane grond "}; // 0x05
const unsigned char man_bed[32] = {"Lift wordt hand- matig bediend "}; // 0x06
const unsigned char Alarmtxt[32] = {"ALARM! STAY CALM LIFT VERPLAATST"}; // 0x07
const unsigned char Noodstoptxt[32] = {"NOODSTOP HULP IS ONDERWEG"}; // 0x08
const unsigned char omhoog[32] = {" Lift gaat OMHOOG "}; // 0x09
const unsigned char omlaag[32] = {" Lift gaat OMLAAG "}; // 0x10
void lcd_send_cmd (unsigned char data, unsigned char adres) //versturen van een instructie naar een display
{
unsigned char data_l, data_u; //Benodigd omdat display in 4-wire mode wordt gebruikt. Eerst sturen van b7-b4, vervolgd door b3-b0
data_l = (data<<4)&0xf0; //select lower nibble by moving it to the upper nibble position
data_u = data&0xf0; //select upper nibble
i2c_start(); //opstarten van i2c communicatie
i2c_write(address[adres]); //selecteren van de juiste display adres
i2c_write(data_u|0x0C); //enable =1 and rs =0
i2c_write(data_u|0x08); //enable =0 and rs =0
i2c_write(data_l|0x0C); //enable =1 and rs =0
i2c_write(data_l|0x08); //enable =0 and rs =0
i2c_stop(); //stoppen van i2c communicatie
delay_ms(5); //benodigd om het proces te vertragen
}
void lcd_send_data (unsigned char data, unsigned char adres) //versturen van de karakters naar een display (Display Data RAM (DDRAM)
{
unsigned char data_l, data_u; //Benodigd omdat display in 4-wire mode wordt gebruikt. Eerst sturen van b7-b4, vervolgd door b3-b0
data_l = (data<<4)&0xf0; //select lower nibble by moving it to the upper nibble position
data_u = data&0xf0; //select upper nibble
i2c_start(); //opstarten van i2c communicatie
i2c_write(address[adres]); //selecteren van de juiste display adres
i2c_write(data_u|0x0D); //enable =1 and rs =1
i2c_write(data_u|0x09); //enable =0 and rs =1
i2c_write(data_l|0x0D); //enable =1 and rs =1
i2c_write(data_l|0x09); //enable =0 and rs =1
i2c_stop(); //stoppen van i2c communicatie
delay_ms(5); //benodigd om het proces te vertragen
}
void lcd_init (unsigned char adres) //Klaarzetten van het display
{
lcd_send_cmd (0x02, adres); //Return Home instructie
lcd_send_cmd (0x28, adres); //4-bit bus mode, 2-line display mode en 5x8 dots display format
lcd_send_cmd (0x0C, adres); //Display aan, Cursor aan, knipperende Cursor uit
lcd_send_cmd (0x80, adres); //Zet cursor op punt 0,0 van het display
delay_ms(5); //benodigd om het proces te vertragen
}
void Display (char keuze){ // void voor woorden naar LCD te sturen
if (keuze == 0x00)
{
lcd_init(LCD); //initialiseren van LCD
for (j = 0; j < 33; j++) //for loop die het woord afloopt
{
lcd_send_data(begane_grond[j],LCD);
if (j > 14 && j < 16) //Als j gelijk is aan 15, naar volgende regel van lcd
{
lcd_send_cmd (0xC0,LCD); //Zet display op de 2de regel
}
}
}
}
if (keuze == 0x01)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(eerste_ver[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x02)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(tweede_ver[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x03)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(derde_ver[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x04)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(begin[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
if (j > 19 && j < 25)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
delay_ms(500);
keuze = 0x05;
}
if (keuze == 0x05)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(Naarbeganegrond[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x06)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(man_bed[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x07)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(Alarmtxt[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x08)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(Noodstoptxt[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x09)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(omhoog[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
if (keuze == 0x10)
{
lcd_init(LCD);
for (j = 0; j < 33; j++)
{
lcd_send_data(omlaag[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
}
void locatie()
{
if (input(PIN_A2) == 0){v = 0;}
if (input(PIN_A3) == 0){v = 1;}
if (input(PIN_A4) == 0){v = 2;}
if (input(PIN_A5) == 0){v = 3;}
}
void pwm_up()
{
for (i=0; i<254; i++)
{
if (i == 253) break;
set_pwm1_duty(i);
delay_ms(3);
}
}
void pwm_down()
{
for (i = 255; i > 75; i--)
{
if (i == 76) break;
set_pwm1_duty(i);
delay_ms(3);
}
}
void handmatig()
{
Display(0x06);
if (input(PIN_A0) == 1 && input(PIN_A1) == 0 )
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
while (input(PIN_A0) == 1 && input(PIN_A1) == 0 )
{
i = 255;
delay_ms(5);
set_pwm1_duty(i);
if (input(PIN_A0) == 0)
{
pwm_down();
delay_ms(2);
output_low(PIN_C1);
delay_ms(2);
set_pwm1_duty(0);
}
}
}
if (input(PIN_A1) == 1 && input(PIN_A0) == 0 )
{
output_low(PIN_C1);
delay_ms(10);
pwm_up() ;
while (input(PIN_A1) == 1 && input(PIN_A0) == 0 )
{
i = 255;
delay_ms(2);
set_pwm1_duty(i);
if (input(PIN_A1) == 0)
{
pwm_down();
delay_ms(2);
set_pwm1_duty(0);
}
}
}
}
void beganegrond()
{
if (input(PIN_D7) == 1 && v > 0 )
{
output_low(PIN_C1);
r = 0; //0 dus de lift moet omlaag
Display(0x10);
delay_ms(5);
pwm_up();
while (input(PIN_A2) == 1)
{
set_pwm1_duty (100);
a = 1;
if (input(PIN_A2) == 0) break;
}
Display(0x00);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A2) == 1 && a == 1)
{
set_pwm1_duty(30);
if (input(PIN_A2) == 0)
{
a = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void verdieping1()
{
if (input(PIN_D6) == 1 && v > 1 || input(PIN_D5) == 1 && v > 1) // verdieping is groter dan 1 dus lift moet naar beneden
{
output_low(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x10);
if (input(PIN_D6) == 1) r = 0;
while (input(PIN_A3) == 1)
{
set_pwm1_duty (100);
b = 1;
if (input(PIN_A3) == 0) break;
}
Display(0x01);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A3) == 1 && b == 1)
{
set_pwm1_duty(30);
Display(0x01);
if (input(PIN_A3) == 0)
{
b = 2;
set_pwm1_duty (0);
break;
}
}
}
else if (input(PIN_D6) == 1 && v < 1 || input(PIN_D5) == 1 && v < 1)
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x09);
while (input(PIN_A3) == 1)
{
set_pwm1_duty (100);
b = 1;
if (input(PIN_A3) == 0) break;
}
Display(0x01);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A3) == 1 && b == 1)
{
set_pwm1_duty(30);
Display(0x01);
if (input(PIN_A3) == 0)
{
b = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void verdieping2()
{
if (input(PIN_D4) == 1 && v > 2 || input(PIN_D3) == 1 && v > 2)
{
output_low(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x10);
while (input(PIN_A4) == 1)
{
set_pwm1_duty (100);
c = 1;
if (input(PIN_A4) == 0) break;
}
Display(0x02);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A4) == 1 && c == 1)
{
set_pwm1_duty(30);
if (input(PIN_A4) == 0)
{
c = 2;
set_pwm1_duty (0);
break;
}
}
}
else if (input(PIN_D4) == 1 && v < 2 || input(PIN_D3) == 1 && v < 2)
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x09);
while (input(PIN_A4) == 1)
{
set_pwm1_duty (100);
c = 1;
if (input(PIN_A4) == 0) break;
}
Display(0x02);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A4) == 1 && c == 1)
{
set_pwm1_duty(30);
if (input(PIN_A4) == 0)
{
c = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void verdieping3()
{
if (input(PIN_D2) == 1 && v < 3)
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x09);
while (input(PIN_A5) == 1)
{
set_pwm1_duty (100);
d = 1;
if (input(PIN_A5) == 0) break;
}
Display(0x03);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A5) == 1 && d == 1)
{
set_pwm1_duty(30);
if (input(PIN_A5) == 0)
{
d = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void stoplift()
{
if (input(PIN_B0) == 1) // Eindschakelaars
{
set_pwm1_duty(0);
output_low(PIN_C1);
if (input(PIN_B0) == 1 && input(PIN_B1) == 1)
{
output_low(PIN_C1);
delay_ms(50);
set_pwm1_duty(50);
delay_ms(2500);
}
if (input(PIN_B0) == 1 && input(PIN_B2) == 1)
{
output_high(PIN_C1);
delay_ms(50);
set_pwm1_duty(50);
delay_ms(2500);
}
}
}
void start()
{
Display(0x04);
if (x < 2 && input(PIN_A2) == 1)
{
while ( x == 0)
{
output_low(PIN_C1);
set_pwm1_duty(100);
if (input(PIN_A2) == 0) x = 1;
}
delay_ms(10);
if ( input(PIN_A2) == 0 || x == 1)
{
output_low(PIN_C1);
set_pwm1_duty(50);
delay_ms(750);
while (input(PIN_A2)== 1)
{
set_pwm1_duty(25);
delay_ms(5);
if (input(PIN_A2) == 0)
{
set_pwm1_duty(0);
x = 2;
break;
}
}
}
}
}
void noodstop()
{
Display(0x08);
if (input(PIN_B5) == 1)
{
while (input(PIN_B5) == 1)
{
set_pwm1_duty(0);
}
}
if (input(PIN_B5) == 1)
{
while (input(PIN_B5) == 0)
{
handmatig();
}
}
}
void alarm()
{
Display(0x07);
}
#INT_EXT
void stoplift();
void main()
{
enable_interrupts(INT_EXT); //Interrupt aanzetten van externe pin B0
setup_timer_2(T2_DIV_BY_16,255,1);
setup_CCP1(CCP_PWM);
output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C2);
set_pwm1_duty(0);
a, b, c, d = 0;
i = 0;
x = 0;
// start();
while(input(PIN_D0) == 0 || input(PIN_D1) == 0)
{
locatie();
if (input(PIN_D0) == 1) noodstop();
else if (input(PIN_D1) == 1) alarm();
else if (input(PIN_B5) == 0)
{
handmatig();
set_pwm1_duty(0);
}
else
{
set_pwm1_duty(0);
delay_ms(5);
locatie();
beganegrond();
verdieping1();
verdieping2();
verdieping3();
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Jan 25, 2023 11:51 am |
|
|
There is a big issue with your counts.
Your 32 character arrays have entries from 0 to 31. You try to send
characters from 0 to 32....
You are enabling INT_EXT, but not INT_GLOBAL.
I don't see how the code ensures that lcd_init will be called?. This
needs to happen at the start after a few hundred mSec to allow the
display to wake. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 479 Location: Montenegro
|
|
Posted: Wed Jan 25, 2023 12:28 pm |
|
|
Code: |
void Display (char keuze)
|
You could shorten your code by doing lcd_init(LCD); only once when you call the function, before you check keuze. Using else if instead of if. This way you are always checking everything, even if the first if was already true. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Jan 25, 2023 1:01 pm |
|
|
Usually an 'lcdinit()' function is only called once at the beginning of main() to setup the LCD module as required( say put into 4bit mode ). At least that's how all the 'HD44780' (?) modules are done.
Perhaps post the make/model info of the I2C LCD module you're using, to help us, help you. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 479 Location: Montenegro
|
|
Posted: Wed Jan 25, 2023 1:08 pm |
|
|
Quote: |
Usually an 'lcdinit()' function is only called once at the beginning of main() to setup the LCD module as required( say put into 4bit mode ). |
Exactly. I noticed no LCD driver library is used and didn't follow it. Normally this is done so, and then just call something like LcdClear to erase everything and get back to a starting position for a new write. No need to set the mode every time. |
|
|
rlabee
Joined: 25 Jan 2023 Posts: 7
|
|
Posted: Thu Jan 26, 2023 12:47 am |
|
|
Hi all,
Thanks for replying and helping me out with this.
So to start
Quote: | Your 32 character arrays have entries from 0 to 31. You try to send
characters from 0 to 32.... |
you're right about this one, didn't notice this and changed it.
Quote: | You are enabling INT_EXT, but not INT_GLOBAL. |
I've put int global in the code to, but is there anymore i need to add? I thought, because it has no further function with it, i could let it out.
Quote: | I don't see how the code ensures that lcd_init will be called?. This
needs to happen at the start after a few hundred mSec to allow the
display to wake. |
I've put the LCD_init() at the front in main now. I called lcd_init() every time when it needed to send something to the display, just like PrinceNai and temtronic said.
Quote: | Using else if instead of if. |
Thanks for this tip!
Quote: | Perhaps post the make/model info of the I2C LCD module you're using, to help us, help you. |
I don't now exactly what you mean with this, but i'm using a PCF8574 I2C LCD module.
Quote: | Exactly. I noticed no LCD driver library is used and didn't follow it. Normally this is done so, and then just call something like LcdClear to erase everything and get back to a starting position for a new write. No need to set the mode every time. |
That's true, I didn't use a LCD driver library because the flex_lcd driver on this forum didn't seem to work for me.
I will look into this option again and try to make it work.
so the new code right now is:
Code: | #include <16f877a.h>
#include <STDIO.h>
#fuses HS,NOWDT
#use delay(clock=20000000)
#use I2C(MASTER, SCL=PIN_C3, SDA=PIN_C4, SLOW)
int16 a, b, c, d, i, r, v, x;
char j;
unsigned char address[1] = {0x4E}; //adres van de 2x16 segment display
unsigned char LCD = 0;
const unsigned char begane_grond[32] = {"Begane grond "}; // 0x00
const unsigned char eerste_ver[32] = {"Verdieping 1 "}; // 0x01
const unsigned char tweede_ver[32] = {"Verdieping 2 "}; // 0x02
const unsigned char derde_ver[32] = {"Verdieping 3 "}; // 0x03
const unsigned char begin[32] = {" Lift start op "}; // 0x04
const unsigned char Naarbeganegrond[32] = {"Lift gaat naar Begane grond "}; // 0x05
const unsigned char man_bed[32] = {"Lift wordt hand- matig bediend "}; // 0x06
const unsigned char Alarmtxt[32] = {"ALARM! STAY CALM LIFT VERPLAATST"}; // 0x07
const unsigned char Noodstoptxt[32] = {"NOODSTOP HULP IS ONDERWEG"}; // 0x08
const unsigned char omhoog[32] = {" Lift gaat OMHOOG "}; // 0x09
const unsigned char omlaag[32] = {" Lift gaat OMLAAG "}; // 0x10
void lcd_send_cmd (unsigned char data, unsigned char adres) //versturen van een instructie naar een display
{
unsigned char data_l, data_u; //Benodigd omdat display in 4-wire mode wordt gebruikt. Eerst sturen van b7-b4, vervolgd door b3-b0
data_l = (data<<4)&0xf0; //select lower nibble by moving it to the upper nibble position
data_u = data&0xf0; //select upper nibble
i2c_start(); //opstarten van i2c communicatie
i2c_write(address[adres]); //selecteren van de juiste display adres
i2c_write(data_u|0x0C); //enable =1 and rs =0
i2c_write(data_u|0x08); //enable =0 and rs =0
i2c_write(data_l|0x0C); //enable =1 and rs =0
i2c_write(data_l|0x08); //enable =0 and rs =0
i2c_stop(); //stoppen van i2c communicatie
delay_ms(5); //benodigd om het proces te vertragen
}
void lcd_send_data (unsigned char data, unsigned char adres) //versturen van de karakters naar een display (Display Data RAM (DDRAM)
{
unsigned char data_l, data_u; //Benodigd omdat display in 4-wire mode wordt gebruikt. Eerst sturen van b7-b4, vervolgd door b3-b0
data_l = (data<<4)&0xf0; //select lower nibble by moving it to the upper nibble position
data_u = data&0xf0; //select upper nibble
i2c_start(); //opstarten van i2c communicatie
i2c_write(address[adres]); //selecteren van de juiste display adres
i2c_write(data_u|0x0D); //enable =1 and rs =1
i2c_write(data_u|0x09); //enable =0 and rs =1
i2c_write(data_l|0x0D); //enable =1 and rs =1
i2c_write(data_l|0x09); //enable =0 and rs =1
i2c_stop(); //stoppen van i2c communicatie
delay_ms(5); //benodigd om het proces te vertragen
}
void lcd_init (unsigned char adres) //Klaarzetten van het display
{
lcd_send_cmd (0x02, adres); //Return Home instructie
lcd_send_cmd (0x28, adres); //4-bit bus mode, 2-line display mode en 5x8 dots display format
lcd_send_cmd (0x0C, adres); //Display aan, Cursor aan, knipperende Cursor uit
lcd_send_cmd (0x80, adres); //Zet cursor op punt 0,0 van het display
delay_ms(5); //benodigd om het proces te vertragen
}
void Display (char keuze){ // void voor woorden naar LCD te sturen
if (keuze == 0x00)
{
for (j = 0; j < 32; j++) //for loop die het woord afloopt
{
lcd_send_data(begane_grond[j],LCD);
if (j > 14 && j < 16) //Als j gelijk is aan 15, naar volgende regel van lcd
{
lcd_send_cmd (0xC0,LCD); //Zet display op de 2de regel
}
}
}
}
else (keuze == 0x01)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(eerste_ver[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x02)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(tweede_ver[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x03)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(derde_ver[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x04)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(begin[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
if (j > 19 && j < 25)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
delay_ms(500);
keuze = 0x05;
}
else (keuze == 0x05)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(Naarbeganegrond[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x06)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(man_bed[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x07)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(Alarmtxt[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x08)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(Noodstoptxt[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x09)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(omhoog[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
else (keuze == 0x10)
{
for (j = 0; j < 32; j++)
{
lcd_send_data(omlaag[j],LCD);
if (j > 14 && j < 16)
{
lcd_send_cmd (0xC0,LCD);
}
}
}
}
}
void locatie()
{
if (input(PIN_A2) == 0){v = 0;}
if (input(PIN_A3) == 0){v = 1;}
if (input(PIN_A4) == 0){v = 2;}
if (input(PIN_A5) == 0){v = 3;}
}
void pwm_up()
{
for (i=0; i<254; i++)
{
if (i == 253) break;
set_pwm1_duty(i);
delay_ms(3);
}
}
void pwm_down()
{
for (i = 255; i > 75; i--)
{
if (i == 76) break;
set_pwm1_duty(i);
delay_ms(3);
}
}
void handmatig()
{
Display(0x06);
if (input(PIN_A0) == 1 && input(PIN_A1) == 0 )
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
while (input(PIN_A0) == 1 && input(PIN_A1) == 0 )
{
i = 255;
delay_ms(5);
set_pwm1_duty(i);
if (input(PIN_A0) == 0)
{
pwm_down();
delay_ms(2);
output_low(PIN_C1);
delay_ms(2);
set_pwm1_duty(0);
}
}
}
if (input(PIN_A1) == 1 && input(PIN_A0) == 0 )
{
output_low(PIN_C1);
delay_ms(10);
pwm_up() ;
while (input(PIN_A1) == 1 && input(PIN_A0) == 0 )
{
i = 255;
delay_ms(2);
set_pwm1_duty(i);
if (input(PIN_A1) == 0)
{
pwm_down();
delay_ms(2);
set_pwm1_duty(0);
}
}
}
}
void beganegrond()
{
if (input(PIN_D7) == 1 && v > 0 )
{
output_low(PIN_C1);
r = 0; //0 dus de lift moet omlaag
Display(0x10);
delay_ms(5);
pwm_up();
while (input(PIN_A2) == 1)
{
set_pwm1_duty (100);
a = 1;
if (input(PIN_A2) == 0) break;
}
Display(0x00);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A2) == 1 && a == 1)
{
set_pwm1_duty(30);
if (input(PIN_A2) == 0)
{
a = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void verdieping1()
{
if (input(PIN_D6) == 1 && v > 1 || input(PIN_D5) == 1 && v > 1) // verdieping is groter dan 1 dus lift moet naar beneden
{
output_low(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x10);
if (input(PIN_D6) == 1) r = 0;
while (input(PIN_A3) == 1)
{
set_pwm1_duty (100);
b = 1;
if (input(PIN_A3) == 0) break;
}
Display(0x01);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A3) == 1 && b == 1)
{
set_pwm1_duty(30);
Display(0x01);
if (input(PIN_A3) == 0)
{
b = 2;
set_pwm1_duty (0);
break;
}
}
}
else if (input(PIN_D6) == 1 && v < 1 || input(PIN_D5) == 1 && v < 1)
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x09);
while (input(PIN_A3) == 1)
{
set_pwm1_duty (100);
b = 1;
if (input(PIN_A3) == 0) break;
}
Display(0x01);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A3) == 1 && b == 1)
{
set_pwm1_duty(30);
Display(0x01);
if (input(PIN_A3) == 0)
{
b = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void verdieping2()
{
if (input(PIN_D4) == 1 && v > 2 || input(PIN_D3) == 1 && v > 2)
{
output_low(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x10);
while (input(PIN_A4) == 1)
{
set_pwm1_duty (100);
c = 1;
if (input(PIN_A4) == 0) break;
}
Display(0x02);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A4) == 1 && c == 1)
{
set_pwm1_duty(30);
if (input(PIN_A4) == 0)
{
c = 2;
set_pwm1_duty (0);
break;
}
}
}
else if (input(PIN_D4) == 1 && v < 2 || input(PIN_D3) == 1 && v < 2)
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x09);
while (input(PIN_A4) == 1)
{
set_pwm1_duty (100);
c = 1;
if (input(PIN_A4) == 0) break;
}
Display(0x02);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A4) == 1 && c == 1)
{
set_pwm1_duty(30);
if (input(PIN_A4) == 0)
{
c = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void verdieping3()
{
if (input(PIN_D2) == 1 && v < 3)
{
output_high(PIN_C1);
delay_ms(5);
pwm_up();
Display(0x09);
while (input(PIN_A5) == 1)
{
set_pwm1_duty (100);
d = 1;
if (input(PIN_A5) == 0) break;
}
Display(0x03);
set_pwm1_duty (50);
delay_ms(250);
while (input(PIN_A5) == 1 && d == 1)
{
set_pwm1_duty(30);
if (input(PIN_A5) == 0)
{
d = 2;
set_pwm1_duty (0);
break;
}
}
}
}
void stoplift()
{
if (input(PIN_B0) == 1) // Eindschakelaars
{
set_pwm1_duty(0);
output_low(PIN_C1);
if (input(PIN_B0) == 1 && input(PIN_B1) == 1)
{
output_low(PIN_C1);
delay_ms(50);
set_pwm1_duty(50);
delay_ms(2500);
}
if (input(PIN_B0) == 1 && input(PIN_B2) == 1)
{
output_high(PIN_C1);
delay_ms(50);
set_pwm1_duty(50);
delay_ms(2500);
}
}
}
void start()
{
Display(0x04);
if (x < 2 && input(PIN_A2) == 1)
{
while ( x == 0)
{
output_low(PIN_C1);
set_pwm1_duty(100);
if (input(PIN_A2) == 0) x = 1;
}
delay_ms(10);
if ( input(PIN_A2) == 0 || x == 1)
{
output_low(PIN_C1);
set_pwm1_duty(50);
delay_ms(750);
while (input(PIN_A2)== 1)
{
set_pwm1_duty(25);
delay_ms(5);
if (input(PIN_A2) == 0)
{
set_pwm1_duty(0);
x = 2;
break;
}
}
}
}
}
void noodstop()
{
Display(0x08);
if (input(PIN_B5) == 1)
{
while (input(PIN_B5) == 1)
{
set_pwm1_duty(0);
}
}
if (input(PIN_B5) == 1)
{
while (input(PIN_B5) == 0)
{
handmatig();
}
}
}
void alarm()
{
Display(0x07);
}
#INT_EXT
void stoplift();
void main()
{
enable_interrupts(GLOBAL); //Global interrupts aanzetten
enable_interrupts(INT_EXT); //Interrupt aanzetten van externe pin B0
setup_timer_2(T2_DIV_BY_16,255,1);
setup_CCP1(CCP_PWM);
output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C2);
set_pwm1_duty(0);
delay_ms(200);
lcd_init(); // Initialiseer LCD
delay_ms(500);
a, b, c, d = 0;
i = 0;
x = 0;
// start();
while(input(PIN_D0) == 0 || input(PIN_D1) == 0)
{
locatie();
if (input(PIN_D0) == 1) noodstop();
else if (input(PIN_D1) == 1) alarm();
else if (input(PIN_B5) == 0)
{
handmatig();
set_pwm1_duty(0);
}
else
{
set_pwm1_duty(0);
delay_ms(5);
locatie();
beganegrond();
verdieping1();
verdieping2();
verdieping3();
}
}
} |
sorry for the long post, my program seems to be huge. That's why i'm happy with all the tips and tricks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
|
rlabee
Joined: 25 Jan 2023 Posts: 7
|
|
Posted: Thu Jan 26, 2023 2:35 am |
|
|
Thanks for the i2c link.
The only problem is that it isn't working for me.
even the test program given with the i2c flex driver wont work. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Jan 26, 2023 2:40 am |
|
|
Are you sure your I2C LCD, is a 5v device?. a lot are not.
Post the part number of the LCD, a preferably a link to it's datasheet. |
|
|
rlabee
Joined: 25 Jan 2023 Posts: 7
|
|
Posted: Thu Jan 26, 2023 2:47 am |
|
|
Well, i have multiple LCD laying around. To test I use the one that came with the easypic v7 (which works on i2c before the program got stuck).
I also have a HD44780 https://www.sparkfun.com/datasheets/LCD/HD44780.pdf
which also worked before the program got stuck. So i hope it isn't because i have the wrong lcd's. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Jan 26, 2023 3:07 am |
|
|
I'm now really puzzled. The standard LCD on an EasyPIC, is not an I2C LCD,
but a standard parallel one.
The setup for the EZ-Flex driver for this would be:
Code: |
#define LCD_DB4 PIN_B0
#define LCD_DB5 PIN_B1
#define LCD_DB6 PIN_B2
#define LCD_DB7 PIN_B3
#define LCD_RS PIN_B4
#define LCD_RW PIN_A0 //Not used
#define LCD_E PIN_B5
//#define USE_RW_PIN 1
|
It'd want the standard EZ-Flex driver code.
I cannot see how your display could have worked!... |
|
|
rlabee
Joined: 25 Jan 2023 Posts: 7
|
|
Posted: Thu Jan 26, 2023 3:13 am |
|
|
This one came with it
https://www.mikroe.com/lcd-2x16-blue
And it still works with my beginning code (not the one on this page). My other LCD also works with my beginning code through i2c. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Jan 26, 2023 4:17 am |
|
|
That is _not_ an I2C LCD. Cannot work with I2C code.
You must have been using some other code without realising it.
Possibly loading the compilers own LCD library for example. |
|
|
rlabee
Joined: 25 Jan 2023 Posts: 7
|
|
Posted: Thu Jan 26, 2023 4:55 am |
|
|
oh, that's strange.
maybe that's the reason the program gets stuck?
Which LCD is the best, in your opinion, to work with? |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 479 Location: Montenegro
|
|
Posted: Thu Jan 26, 2023 8:06 am |
|
|
You are not using "else if" in your function. I really don't know how a sequence of elses only work. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|