KlausST
Advanced Member level 7
- Joined
- Apr 17, 2014
- Messages
- 26,356
- Helped
- 4,926
- Reputation
- 9,875
- Reaction score
- 5,803
- Trophy points
- 1,393
- Activity points
- 177,461
bit oldstate_00;
bit oldstate_01;
sbit white at PORTE.B0;
sbit green at PORTE.B2;
char count;
char temporary = 0;
const char text1[] = "I did it";
void blink(char i){ // for debug, blink led x times
char j;
for (j=0;j<i;j++){
white = ~white;
Delay_ms(50);
}
if (white == 1){
white = 0;}
}
void rotary(){ // basic rotary encoder function for turning left and right
char temp;
temp = PORTC & 0b00000011;
if (temp == 1){
count++;
Delay_ms(77);
}
if (temp == 2){
count--;
Delay_ms(77);
}
}
void min_max(char limit){ // set max count so i dont have to turn so far
if (count >= 250){
count = 0;}
if (count >= limit){
if (count <= 249){
count = limit;
}
}
}
void init_LCD(){ // this wont work, only works when this code is in void main... not as a function
I2C1_Start(); // I2C start signal
I2C1_Wr(0x78); // address 0X78
I2C1_Wr(0x00); // control byte
I2C1_Wr(0x38); // function set
I2C1_Wr(0x39); // function set
I2C1_Wr(0x14); // bias
I2C1_Wr(0x78); // contrast set
I2C1_Wr(0x5E); // power/icon/contrast control
I2C1_Wr(0x6D); // follower control
I2C1_Wr(0x0C); // display on
I2C1_Wr(0x01); // clear display
I2C1_Wr(0x06); // entry mode set
I2C1_Stop(); // I2C stop signal
}
void Show(unsigned char *text){ // attemping to print to lcd
int n;
int result;
result = strlen(text);
I2C1_Start();
I2C1_Wr(0x78);
I2C1_Wr(0x40);
I2C1_Repeated_Start();
for(n=0;n < result + 1;n++){
I2C1_Wr(*text);
++text;
}
I2C_Stop();
}
void hello(){ // attemping to print to lcd
I2C1_Start();
I2C1_Wr(0x78);
I2C1_Wr(0x40);
I2C1_Repeated_Start();
I2C1_Wr(count + 48);
I2C1_Stop();
}
void output_count(){ // attemping to print to lcd
if (temporary != count){
temporary = count;
I2C1_Start();
I2C1_Wr(0x78);
I2C1_Wr(0xC0);
I2C1_Wr(count + 48);
I2C1_Stop();
UART1_Write(0x0D);
UART1_Write(0x0A);
UART1_Write(count+48);
UART1_Write(0x0D);
UART1_Write(0x0A); }
}
void main() {
// setting up 18f4523 for use
INTCON = 0b11100000; INTCON2.B7 = 1; IPEN_bit = 0;
PIE1 = 0b00001000; SSPIE_bit = 1;
TRISA = 0; PORTA = 0; LATA = 0; TRISB = 0; PORTB = 0; LATB = 0;
TRISC = 0b0111111; PORTC = 0; LATC = 0;
TRISD = 0b00000001; PORTD = 0; LATD = 0;
TRISE = 0b00000010; PORTE = 0; LATE = 0;
TMR0ON_bit = 0; TMR1ON_bit = 0; TMR2ON_bit = 0; TMR3ON_bit = 0;
CCP1CON = 0; CCP2CON = 0;
TXEN_bit = 0; SPEN_bit = 0; CREN_bit = 0;
ADON_bit = 0; ADCON0 = 0; ADCON1 = 0x0F;
CMCON = 0x07; CVREN_bit = 0; CVROE_bit = 0;
HLVDEN_bit = 0;
/* should be taken care of by hardware i2c
SSPSTAT = 0;
SSPEN_bit = 1; SSPCON1 = 0b00101000; //SSPCON1.B3 = 1; SSPCON1.B2 = 0; SSPCON1.B1 = 0; SSPCON1.B0 = 0;
BRG16_bit = 0x18;SSPADD = 0x63; */
// initial variable declaration
white = 0; green = 0; oldstate_00 = 0; oldstate_01 = 0;
count = 0; count_temp = 0; i = 1; temporary = count;
blink(10); green = 1;
UART1_Init(115200);
Delay_ms(100);
I2C1_Init(100000); // initialize I2C communication
Delay_ms(500);
/*init_LCD();*/ // THIS DOESNT WORK HERE FOR SOME REASON
I2C1_Start(); // I2C start signal
I2C1_Wr(0x78); // address 0X78
I2C1_Wr(0x00); // control byte
I2C1_Wr(0x38); //Delay_us(250); // function set
I2C1_Wr(0x39); //Delay_ms(2); // function set
I2C1_Wr(0x14); // bias
I2C1_Wr(0x78); // contrast set
I2C1_Wr(0x5E); //Delay_ms(2); // power/icon/contrast control
I2C1_Wr(0x6D); // follower control
I2C1_Wr(0x0C); // display on
I2C1_Wr(0x01); // clear display
I2C1_Wr(0x06); //Delay_ms(2); // entry mode set
I2C1_Stop(); // I2C stop signal
Delay_ms(100);
blink(7); green = 0;
do{
rotary();
min_max(9);
output_count();
if (Button(&PORTE,1,1,1)){ // The lonely button
oldstate_00 = 1;
white = 1;}
if (oldstate_00 && Button(&PORTE,1,1,0)){
oldstate_00 = 0;
white = 0;
show("i did it");
}
if (Button(&PORTD,0,1,0)){ // rotary encoder button
oldstate_01 = 1;
white = 1;}
if (oldstate_01 && Button(&PORTD,0,1,1)){
oldstate_01 = 0;
white = 0;
UART1_Write(count + 48);
UART1_Write(0x0D); UART1_Write(0x0A);
hello();
}
}while(1);
}
Yes, it seems you missed one of the most basic rules for I2C: Never drive the signals HIGH. Neither SCL nor SDA.It looks like i am getting ack on every byte but i cannot seem to send data, i have tried and read so much. i feel like its something simple im missing. i
...What confuses me is that, why does the init_lcd not work as a function but seems to initialize the lcd while that code is place direcrly in main?
Hello,
where is defined the device adress ?
slave=0x00 ?
must be assigned into the beginning of main ()
After it goes low .... thats the point ... then it IS low....both I2C signals .... then starts the communication....AFTER it goes low, it gets captured by the scope,
1.3V is not a validBut if 1.3 is low enough logic level,
Are you saying that your oscilloscope is displaying captured data differently than any oscilloscope on the market, hence the shown waveform isn't real? Hard to believe.it does not start at low level, AFTER it goes low, it gets captured by the scope, that is what you see. I will look into level converter, I am sure the 1.3v ahould be 0v. But if 1.3 is low enough logic level, wouldnt it still be interpreted as a zero logic. Also, if the ack is not correct (i.e. no ack) i thought the library would not continue transmision of bytes. That you for your time.
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?