I assumed that the OP has no Sony TV in the same, or nearby room already. But would have been a good idea to check, admittedly.What happen if someone else use his TV remote and start playing with yours fans?
#define fan RC0_bit
#define lamp1 RC1_bit
#define lamp2 RC2_bit
sbit ir_rx at RA2_bit; // IR receiver 40KHz
sbit ir_in_direction at TRISA2_bit; // Infrared receiver input
// function prototypes
void get_mark_time(void); // get Sony IR mark time
// global variables
char counter = 0;
char shadow = 0;
char bitcount;
char ir_address;
char ir_command;
int mark_time;
int fan_mask;
unsigned int num,Cnt_T1;
void interrupt()
{
while(TMR0IF_bit)// TMR0 interrupt sub routine
{
counter++; // increment counter
TMR0IF_bit = 0; // Clear Timer0 overflow interrupt flag
break;
}
while(TMR1IF_bit)
{
TMR1IF_bit = 0;
TMR1H = 0xFE;
TMR1L = 0xE9; // 1ms count
RAIE_bit = 0;//disable IOC interrupt
if(!fan_mask)
{
// code
Cnt_T1++;
T0IE_bit = 0; // Timer0 interrupt ebable
while(Cnt_T1 >= 0 && Cnt_T1 < num)
{
fan = 1;
break;
}
while(Cnt_T1 >= num && Cnt_T1 <= 28)
{
fan = 0;
break;
}
while(Cnt_T1>28)
{
fan = 1;
TMR1IE_bit = 0; // disable timer1 interrupt
RAIE_bit = 1;//Enable IOC interrupt
T0IE_bit = 1; // Timer0 interrupt ebable
break;
}
}
else
{
fan = 1;
}
break;
}
while(RAIF_bit)// IOC subroutine
{
RAIF_bit = 0;// clear IOC_Flag
TMR1IE_bit = 1; // enable timer1 interrupt
Cnt_T1 = 0;
break;
}
}// interrupt
void GetData()
{
ir_command = 0; // initialise to prevent false trigger
ir_address = 0; // initialise to prevent false trigger
get_mark_time(); // get Sony leader - 2.4mS Mark, 1.2mS space
if ((mark_time > 120) && (mark_time < 200)) // set the frequency limit
{ // ignore anything but 2.4mS mark
for(bitcount = 0 ; bitcount < 7 ; bitcount++)
{ // 7 bit command
get_mark_time(); // get a Sony IR bit
ir_command >>= 1; // shift
ir_command &= 0x7f; // top bit zero
if (mark_time > 0x40)
{ // > 40 is assumed to be a 1
ir_command ^= 0x80; // top bit 1
}
}
ir_command >>= 1; // shift 1 unused bit
ir_command &= 0x7F; // clear top bit
for(bitcount = 0 ; bitcount < 5 ; bitcount++)
{ // 5 bit address
get_mark_time(); // get a Sony IR bit
ir_address >>= 1; // shift
ir_address &= 0x7f; // top bit zero
if (mark_time > 0x40)
{
ir_address ^= 0x80; // top bit 1
}
}
ir_address >>= 3; // shift 3 unused bits
ir_address &= 0x1F; // clear top 3 bits
}
}// GetData
void main()
{
TRISA = 0b00000111;
TRISC = 0x00;
PORTC = 0x00;
ANSEL = 0x00;
CMCON = 0x07;
OPTION_REG = 0x03; // TMR0 prescaler set to 1:16
// start timer 0 counting
GIE_bit = 1; // Global interrupt enable
T0IE_bit = 1; // Timer0 interrupt ebable
ir_in_direction = 1;// set IR dirrecting
IOCA1_bit = 1; // Enable IOC at RA2
RAIE_bit = 1; // Enable IOC interrupt
RAIF_bit = 0; // Clear RAIF bit
// Timer1 settings
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xFE;
TMR1L = 0xE9; // 1ms count // 1ms count
TMR1IE_bit = 1;
PEIE_bit = 1;
Delay_ms(500);
while(1)
{
GetData();
num = EEPROM_Read(5);
Delay_ms(20);
if(ir_address == 1)
{ // TV
if(ir_command == 0)
{ // button 1
num = 24;
fan_mask=0;
}
if(ir_command == 1)
{ // button 2
num = 22;
fan_mask=0;
}
if(ir_command == 2)
{ // button 3
//shadow ^= 0x10; // toggle output 4
num = 20;
fan_mask=0;
}
if(ir_command == 3)
{ // button 3
//shadow ^= 0x10; // toggle output 4
num = 18;
fan_mask=0;
}
if(ir_command == 4)
{ // button 3
//shadow ^= 0x10; // toggle output 4
num = 15;
fan_mask=0;
}
if(ir_command == 5)
{ // button 3
//shadow ^= 0x10; // toggle output 4
num = 12;
fan_mask=0;
}
if(ir_command == 6)
{ // button 3
//shadow ^= 0x10; // toggle output 4
num = 9;
fan_mask=0;
}
if(ir_command == 7)
{ // button 3
//shadow ^= 0x10; // toggle output 4
num = 4;
fan_mask=0;
}
if(ir_command == 8)
{ // button 3
//shadow ^= 0x10; // toggle output 4
num = 0;
fan_mask=0;
}
if(ir_command == 9)
{ // button 0
//shadow = 0x00; // all outputs off
fan_mask=1;
num = 0;
}
EEPROM_Write(5,num);
Delay_ms(200);
num = EEPROM_Read(5);
Delay_ms(100);
}
}
}
// get time of mark, then ignore space
void get_mark_time(void)
{
while(ir_rx); // wait for a mark
counter=0;
TMR0 = 0;
while(!ir_rx); // wait for space
mark_time = (counter << 8) + TMR0; // collect integer mark time
}
Dear All,
How should we detect the start bit using Timer0 in assembly language with pic16f877a
Thanks in advance
you can try...
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?