AtomSoft
Well-Known Member
Ok need some help. This is not working . I think its the way im shifting the bits. Not sure tho. I know the timing is good. But it doesnt work. A better question is how does one send out LSB first? Here is my code.
Code:
#include <p18f1320.h>
#include <stdio.h>
#include <delays.h>
#pragma config WDT = OFF, OSC = INTIO2, LVP = OFF
#define ir LATBbits.LATB3
#define ir_tris TRISBbits.TRISB3
#define btn1 PORTBbits.RB5
#define btn2 PORTBbits.RB1
#define btn1_tris TRISBbits.TRISB5
#define btn2_tris TRISBbits.TRISB1
//// DELAY TEMPS & CONFIG
char d_tmp; //delay calculation temp
char lt,lt2; //loop temp
void main(void);
void ir_byte(char add, char cmd);
void delay_s(char sec);
void delay_ms(char msec);
void delay_us(char usec);
void main(void){
OSCCON = 0x72; //8MHz
btn1_tris = 1;
btn2_tris = 1;
ir_tris = 0;
ir = 0;
while(1){
if(btn1 == 1){
//Do something
ir_byte(0x0B,0x00); //Actual Command is 0x0D but since this is LSB first its 0x0B
}
if(btn2 == 1){
//Do something
ir_byte(0x0B,0x01);
}
}
}
void ir_byte(char add, char cmd){
char bl; //Byte Loop
char dev;
char com;
///Preperations:
com=cmd<<1;
dev=add<<3;
////////////////////////////////////////
//Send Start////////////////////////////
//turn on for 2.4ms/////////////////////
////////////////////////////////////////
ir = 1;
Delay1KTCYx(4);
Delay100TCYx(8);
ir = 0;
Delay1KTCYx(1);
Delay100TCYx(2);
////////////////////////////////////////
//Send Command//////////////////////////
////////////////////////////////////////
for(bl=0;bl<7;bl++){
ir = 1;
if((com & 0x80) != 0){
// cmd = 1 delay 1.2ms
Delay1KTCYx(2);
Delay100TCYx(4);
} else {
// cmd = 0 delay 600us
Delay1KTCYx(1);
Delay100TCYx(2);
}
//Turn off IR for 600us (space)
ir = 0;
Delay1KTCYx(1);
Delay100TCYx(2);
com=com<<1; //Shift data 1 bit over
}
////////////////////////////////////////
//Send Address//////////////////////////
////////////////////////////////////////
for(bl=0;bl<5;bl++){
ir = 1;
if((dev & 0x80) != 0){
// add = 1 delay 1.2ms
Delay1KTCYx(2);
Delay100TCYx(4);
} else {
// add = 0 delay 600us
Delay1KTCYx(1);
Delay100TCYx(2);
}
//Turn off IR for 600us (space)
ir = 0;
Delay1KTCYx(1);
Delay100TCYx(2);
dev=dev<<1; //Shift data 1 bit over
}
delay_ms(45);
////////////////////////////////////////
///// DONE /////////////////////////////
////////////////////////////////////////
}
void delay_s(char sec)
{
for(lt=0;lt<sec;lt++)
Delay10KTCYx(200);
}
void delay_ms(char msec)
{
for(lt=0;lt<msec;lt++)
Delay1KTCYx(2);
}
void delay_us(char usec)
{
for(lt2=0;lt2<usec;lt2++){
Delay1TCY();
Delay1TCY();
}
}