View previous topic :: View next topic |
Author |
Message |
rsavas
Joined: 10 Aug 2010 Posts: 197 Location: Baldwin, Ontario
|
Posted: Mon Apr 28, 2014 1:38 am Post subject: (AN #157 - Implementation of IR NEC protocol) ported to xmeg |
|
|
Hi guys,
As the subject says I am trying to figure this out, so asking for some help. I have tested some of the code. I am getting the pin change vector code running "Infrared:", well at least flashing a led to see it is running through the code but the other timer stuff is not working or at least I am not getting it to decode and display. It just is reporting 0 address and 0 command.
Sorry that I posted a lot of code but most of it is xm128a1 initialization on my test board.
If we get it worked out, we could add it to the AN if that seems to be the thing to do.
I am back one sw rev but I am sure that ain't the problem. I wish it were that easy
I have a few IR transmitters that I have been trying, a couple of Pioneer (CU-RX008,CU-PD067) and a Channel master model 9537 for the antenna rotor. I have checked the IR receiver, TSOP4836 on the scope and the leading pulse(9ms), gap (4.5ms) match what is documented and the frame length is 67.5ms, so I don't think it is that. I can post the scope traces if it is required.
I'll post the AN#157 code below my code, to make it easier to compare. I have set the xmega timer resolution to match the 8-bit counter in the m8 (Maybe the wrong this to do?). I also set the xm128a1 to run at 8MHz, so the timer load values can be re-used. I verified the clock pre-scalers seem to be correct for the port "D" pwm square-wave function measuring ~977Hz and 50% duty cycle.
Mean while I'll re-read the xmega A user manual as it is something I am doing wrong or misunderstanding about the 16-bit counter operation.
So if anyone can help that would be very much appreciated.
Thanks
Rick
Code: | '*****************************************************************************************************
'* Filename : Get_NEC_IR_xmega_V1.0.BAS *
'* Revision : 1.0 *
'* Function : Program decodes NEC IR protocol *
'* Controller : ATxmega128A1 *
'* References : (AN #157 - Implementation of IR NEC protocol *
' : http://www.sbprojects.net/projects/ircontrol/nec.php *
'* : http://www.circuitvalley.com/2013/09/nec-protocol-ir-infrared-remote-control.html *
'* : http://techdocs.altium.com/display/ADRR/NEC+Infrared+Transmission+Protocol *
'* Author : Rubashka Vasiliy , Ukraine , 2007 *
'* Modify : for xmega, using counter Tcc1 *
'* Date : 2014-Apr-26 *
'* Compiler : BASCOM by MCS Electronics *
'* Compiler ver: 2.0.5.0 or higher(2.0.7.6) *
'* Hardware : Sparkfun xmega100 BOB (ATxmega128A1AU) *
'* 4x40 Character LCD, Newhaven NHD-0440WH-ATMI-JT# [STN-Blue (Negative)] *
'* 4x40 Character LCD, Newhaven NHD-0440AZ-FL-YBW [Yellow/Green LED Backlight] *
'* PGA2310 *
'* PCA9555 *
'* FT232RL USB virtual UART *
'* 3x Bourns PC12 rotary encocders with switches *
'* 6x spst switches with LED (C&K K5V-BU) *
'*****************************************************************************************************
'$regfile = "m8def.dat"
$regfile = "xm128A1def_orig.dat"
$crystal = 8000000
Config Osc = Disabled , 32mhzosc = Enabled '32MHz and 32KHz enabled
Config Sysclock = 32mhz , Prescalea = 4 , Prescalebc = 1_1 'CPU Clock = 8MHz
Config Eeprom = Mapped 'must be used for Xmega EEPROM
'Init and enable the DFLL (Digital Frequency Locked Loop) for automatic run-time calibration of the internal 32MHz Oscillator
Osc_dfllctrl = &B00000000 'The internal 32.768 KHz Oscillator is used for calibration
Set Dfllrc32m_ctrl.0 'Internal RC oscillator 8 MHz
'Config Lcdpin = Pin , Rs = Portb.0 , E = Portb.2 , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7
'Config Lcd = 16 * 2
'this is the customized LCD assembler code to use vport 0 in 4 bit mode, with busy reading
'found in C:\Program Files\MCS Electronics\BASCOM-AVR\LIB
$lib "lcd4e2busy.lib"
'*************
'* Variables *
'*************
Dim Tik As Word 'counter of teaks of timer
Dim Byt As Byte 'counter accepted bit
Dim Repeat_flag As Bit 'flag of repetition
Dim Start_flag As Bit 'flag of start condition
Dim Address As Byte 'byte of address
Dim Command As Byte 'byte of command
Dim Address_1 As Byte 'direct byte of address
Dim Command_1 As Byte 'direct byte of command
Dim Address_0 As Byte 'indirect byte of address
Dim Command_0 As Byte 'indirect byte of command
Dim Summa As Word
'
Dim Lcd_contrast As Word : Lcd_contrast = 4095 'will use DAC to control LCD contrast (Vo)
Dim Sw_version As String * 3 : Sw_version = "1.0"
'
'--------------------------------Configure Port A-------------------------------
'
Config Porta = Input
Porta_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'setup the DACA using porta.2
Config Daca = Enabled , Io0 = Enabled , Channel = Single , Reference = Int1v , Interval = 64 , Refresh = 64
Start Daca
Daca0 = Lcd_contrast '1 V output on porta.2
'Stop Daca ' to diabled it
'
'--------------------------------Configure Port B-------------------------------
'
'Portb = &H00
Config Portb = Output
Portb_pin0ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin1ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin2ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin3ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin4ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin5ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin7ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Lcd_led1 Alias Portb.0 'controls backlight LED
Lcd_led2 Alias Portb.1 'controls backlight LED
'Tone_led Alias Portb.1 'controls Tone Defeat LED
Mute_led Alias Portb.3 'controls Mute LED
Tape_mon_led Alias Portb.4 'controls Tape Monitor LED
Tuner_seek_led Alias Portb.5 'controls Tuner Seek LED
Menu_led Alias Portb.6 'controls Menu LED
Power_led Alias Portb.7 'controls Power/Standby LED
'setup the DACA using portb.2
'Config Dacb = Enabled , Io0 = Enabled , Channel = Single , Reference = Avcc , Interval = 64 , Refresh = 64
'
'-------------------------------Configure Port C--------------------------------
'
'Config TWI,PC.0=SDA,PC.1=SCL
Dim Twi_start As Byte ' This Variable is used by the I2C functions
Open "TWIC" For Binary As # 10 ' Pull-Up resistor (4K7) on SDA and SCL to Vcc = 3,3V
'Portc.0 'SDA Pin of Port C (ATXMEGA192A3)
'Portc.1 'SCL Pin of Port C (ATXMEGA192A3)
Config Twic = 100000 '100KHz clock
I2cinit # 10 'set i2c pins to correct state , open collector , pull up activated
'COM Interface to PC via USB using FT232RL ('com1 is located on Port C,PC2=RXD,PC3=TXD)
'TCC0 is used for the UART
Config Com1 = 38400 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8 '38400 for PC application support
'Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "COM1:" For Binary As # 1
'{027}[K Erase Line:
'{027}[2J Erase Display:
Print # 1 , "{013}" ; "{010}" 'send a <cr> & <lf>
Print # 1 , "Get_NEC_IR_xmega_v" ; Sw_version ; ".BAS" 'start up banner
'CONFIG SPI MASTER on Port C
'PGA2311: MSB first, clocked on rising edge,R7..R0,L7..L0
'
' MOSI, PortC.5: Connect to PGA2311.3 (SDI)
' MISO, PortC.6: Not used
' SCK, PortC.7: Connect to PGA2311.6 (SCLK)
' CS, PortC.4: Connect to PGA2311.2 (~CS)
Config Portc.4 = Output
Pga_cs Alias Portc.4
Set Pga_cs 'Slave Select Pin, Connect to PGA2311-2 (CS), active low
Config Spic = Hard , Master = Yes , Mode = 0 , Clockdiv = Clk32 , Data_order = Msb , Ss = None
'Config Spic = Hard , Master = Yes , Mode = 0 , Clockdiv = Clk64 , Data_order = Msb , Ss = None
Open "SPIC" For Binary As # 2
'Portc_pin0ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'Portc_pin1ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'Portc_pin2ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'Portc_pin3ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portc_pin4ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portc_pin5ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portc_pin7ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'
'-------------------------------Configure Port D--------------------------------
'
Config Portd.0 = Output 'Output for PWM
'Use as a ~1KHz square wave source for testing PGA2310
'Config Tcd0 Prescale = 8 , Wgmode = Pwm
' Select Clock Source for timer/counter0
Tcd0_ctrla = &B0000_0010 'DIV2 --> 8MHz/2 = 4MHz
' <--TCD0_CCA->
' +-----------+ +
' | | |
'------+ +----------+
'
' <--------------------->
' Period = 1.02mSec(977Hz)
'
'Enable PWM Output
Tcd0_ctrlb = &B0001_0_011
' ^ ^
' ^ Waveform Generation Mode = Single Slope PWM
' Enable OD0A ---> PinD.0 for PWM Output
'SET Resolution of PWM (min. = &H0003 ...... max. = &HFFFF)
'Tcd0_per = &HFFFF 'Set Period = FFFF = 65535 --> 65535/4MHz = 16.38mSec(61Hz)
Tcd0_per = &H0FFF 'Set Period = FFF = 4095 --> 4095/4MHz = 1.02mSec(977Hz)
'Change TCD0_CCA (Duty Cycle)
Tcd0_cca = &H07FF '2047/4MHz = 0.512mSec
'Tcd0_cca = 32767 '32767/4MHz = 8.19mSec
'Tcd0_cca = 20000 '20000/4MHz = 5ms
'Tcd0_cca = 10000 '10000/4MHz = 2.5mSec
'
'--------------------------------Configure Port E-------------------------------
'
'Porte_dir = &B1011_1011 'If DIRn is written to one, pin is configured as an output pin.
Porte_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'
'--------------------------------Configure Port F-------------------------------
'Portf_dir = &B0000_0000 'If DIRn is written to one, pin is configured as an output pin.
Config Portf = Input
Sw_enc1 Alias Pinf.0 'switch part of encoder1,toggles between Analog or Digital selection of channels
Sw_enc2 Alias Pinf.1 'switch part of encoder2,toggles between Volume or Balance mode
Sw_enc3 Alias Pinf.2 'switch part of encoder3,used for USB & Tuner
Sw_power_on Alias Pinf.3 'Power on/standby switch
Sw_mute Alias Pinf.4 'Mute Switch
Sw_tape_mon Alias Pinf.5 'Tape monitor switch
Sw_tuner_seek Alias Pinf.6 'Tuner_seek switch
Sw_menu Alias Pinf.7 'Menu switch
Portf_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'------------------------------Configure Port H---------------------------------
Config Vport0 = H 'Real Port Direction
Ddrh = &B1111_1111
Vport0_dir = &B1111_1111 'Virtual Port 0 = OUTPUT
'Porth_dir = &B1011_1011 'If DIRn is written to one, pin is configured as an output pin.
'---------------------------------LCD Setup-------------------------------------
'use lcd4e2busy.lib and define port H as vport 0. The lib routines will control the port pin directions thereafter
Porth_pin0ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin1ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin2ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin3ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin4ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin5ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin6ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin7ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Config Lcd = 40 * 2 'Config Lcd = 40 * 2(update 2.0.7.3 :Both are valid, but the * will become invalid in the future.
Dim ___lcde As Byte 'use for two Enable module type,0 = Top 2 rows, 1 = Bottom 2 rows
Initlcd 'same as asm "Rcall _init_lcd"
'Waitms 100
___lcde = 0 'select the upper 2 line controller 'clear the LCD display
Cursor Off Noblink 'hide cursor
Cls
___lcde = 1 'select the lower 2 line controller 'clear the LCD display
Cursor Off Noblink 'hide cursor
Cls
___lcde = 0
'------------------------------Configure Port J---------------------------------
'PortJ_dir = &B0000_1111 'If DIRn is written to one, pin is configured as an output pin.
Config Portj = Output
18v_sw_c Alias Portj.0 'controls Analog Voltage supply
Rf_sw1 Alias Portj.1 'controls RF Band switch diode
Rf_sw2 Alias Portj.2 'controls RF Band switch diode
Ir_pin Alias Pinj.6 'input from TSOP4836
Config Pinj.6 = Input
Portj_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'^ ^
'^ Reaction on falling edge
'^
'Enable Pullup
Portj_int0mask = &B0100_0000 'include PIN6 in INT0 Mask
Portj_intctrl = &B0000_00_11
'^^
'||-High Level INT0 Interrupt
'Config Int0 = Falling 'Interruption on Falling
'On Int0 Infrared 'work on interruption
On Portj_int0 Infrared 'On pin change, vector to interrupt service routine
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Configure Port K; Used for Bourns encoders & PGA MUTE & ZCEN'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'PortK_dir = &B1100_0000 'If DIRn is written to one, pin is configured as an output pin.
Config Portk = Input
Config Pink.6 = Output 'PGA2311 Mute, active Low
Pga2311_mute Alias Portk.6
Set Pga2311_mute
Config Pink.7 = Output 'PGA2311 ZCEN, active High
Pga2311_zen Alias Portk.7
Reset Pga2311_zen
Portk_pin0ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin1ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin2ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin3ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin4ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin5ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin6ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin7ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'----------------------------------Interrupts-----------------------------------
Config Priority = Static , Vector = Application , Hi = Enabled 'Hi = Enabled
'Config Pind.2 = Input
'Config Portc.2 = Output
'Config Timer0 = Timer , Prescale = 256 '8000000/256=31250 Hz(32us)
Config Tcc1 = Normal , Prescale = 256 ', Resolution = Byte
'Stop Timer0
Stop Tcc1
'Enable Timer0
Enable Tcc1_ovf , Hi
'Enable Int0
Enable Portj_int0 , Hi 'Enable this Interrupt as Hi Level Interrupt
Enable Interrupts
'On Timer0 Tikers 'work on timer
On Tcc1_ovf Tikers
'
Lcd_led1 = 0 'controls backlight LED
Lcd_led2 = 1
Mute_led = 1 'controls Mute LED
Tape_mon_led = 1 'controls Tape Monitor LED
Tuner_seek_led = 1 'controls Tuner Seek LED
Menu_led = 1 'controls Menu LED
Power_led = 1
Waitms 500
Mute_led = 0 'controls Mute LED
Tape_mon_led = 0 'controls Tape Monitor LED
Tuner_seek_led = 0 'controls Tuner Seek LED
Menu_led = 0 'controls Menu LED
Power_led = 0
___lcde = 0 : Locate 1 , 1
Lcd "Get_NEC_ir_xmega_v" ; Sw_version ; ".bas " 'start up banner
___lcde = 1 : Locate 1 , 1
'################################################################################################################
Do 'Main cycle
Cls 'Clean LCD
Lcd Address ; " " ; Command 'Lcd ADDRESS and COMMAND
'If Command = 8 Then Portc.2 = 1 'If pressed key "P+" - to include LED
If Command = 8 Then Power_led = 1
'If Command = 240 Then Portc.2 = 0 'If pressed key "P-" - to switch off LED
If Command = 240 Then Power_led = 0
Waitms 10 'Delay 10 ms
Loop
End 'End of main cycle
'################################################################################################################
Tikers : 'work on timer
'Timer0 = 253 '31250/(256-253)=10416,66 Hz (96 us)
Tcc1_per = 253 'Tcc1_cnt = 253
Incr Tik
If Tik >= 1200 Then 'if 1200 teaks, have thrown all in source condition
Tik = 0
Repeat_flag = 0
Start_flag = 0
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
Address = 0
Command = 0
'Stop Timer0
Stop Tcc1
End If
Return
'################################################################################################################
Infrared : 'work on interruption
Power_led = 1 'DEBUG
'Start Timer0
Start Tcc1
If Tik >= 139 And Tik < 150 Then 'if has happenned from 139 before 150 teaks - "START"
Address = 1 '9ms
Repeat_flag = 0
Start_flag = 1
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
End If
If Tik >= 116 And Tik < 139 Then 'if has happenned from 116 before 138 teaks - "REPETITION"
Address = 2
Repeat_flag = 1
Start_flag = 0
End If
If Tik >= 22 And Tik < 116 And Start_flag = 1 Then 'if has happenned from 22 before 115 teaks - have taken "1"
Incr Byt
If Byt < 9 Then
Shift Address_1 , Left
Address_1 = Address_1 + 1
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
Address_0 = Address_0 + 1
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
Command_1 = Command_1 + 1
End If
If Byt >= 25 Then
Shift Command_0 , Left
Command_0 = Command_0 + 1
End If
End If
If Tik >= 10 And Tik < 22 And Start_flag = 1 Then 'if has happenned from 10 before 21 teaks - have taken "0"
Incr Byt
If Byt < 9 Then
Shift Address_1 , Left
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
End If
If Byt >= 25 Then
Shift Command_0 , Left
End If
End If
Tik = 0
If Byt = 32 Then 'if have taken 4 bytes, check correctness a receiving a command
'if address or command 16-bit, check does not pass
'Summa = Address_0 + Address_1
'If Summa = 255 Then
Address = Address_1 ' Address = Address_1
' Else
' Address = 0
'End If
'Summa = Command_0 + Command_1
'If Summa = 255 Then
Command = Command_1 ' Command = Command_1
' Else
' Command = 0
'End If
Byt = 0
Repeat_flag = 0
Start_flag = 0
'Stop Timer0
Stop Tcc1
End If
Portj_intflags = &B0000_00_01 'Clear PortJ Int0 flag,Writing a one to this flag's bit location will clear the flag
Power_led = 0 'DEBUG
Return |
Code: | '****************************************************************************
'* Filename : IR NEC by http://www.sbprojects.com/knowledge/ir/ir.htm *
'* Revision : 1.0 *
'* Controller : ATMEGA8 *
'* Compiler : BASCOM-AVR 1.11.8.3 DEMO *
'* Author : Rubashka Vasiliy , Ukraine , 2007 *
'* WWW : http://ledeffects.net *
'* Mail : info@ledeffects.net *
'****************************************************************************
'* *
'* IR 36kHz ATMEGA 8 *
'* .-O-. .--_/ --. *
'* |___| /-[10k]-|Res | *
'* ||| | | | *
'* /--/|| \--\ | | *
'* | |*--------+---|Pd2 Pc2|----[510]-----|>| ---\ LED *
'* | || | | | | *
'* | |\-[47k]--* | | | *
'* | *--[100]--*---|Vcc GND|--------\ | *
'* *---+---------+---|GND | | | *
'* | | /---+---|Pb6 Vcc|------\ | | *
'* | | | /-+---|Pb7 Pb5|----\ | | | *
'* | | | | | | Pb4|--\ | | | | *
'* | | | | | | | | | | | | *
'* | | | | | | Pb2|\ | | | | | *
'* | | | | | /-|Pb0 | | | | | | | *
'* | | | | | | '-------' | | | | | | *
'* | | | | | --------\ | | | | | | *
'* | | | | -----------+-+-+-+-*-+-----*-----+-----*------\ *
'* | +|10,0 | ----------- \ | | | | | | |0,1 | +|100,0 | *
'* | --- ----------- \ | | | | | | | --- | --- | *
'* | --- | | | | | | | | --- | --- | *
'* | | | | | | | | | | | | | | *
'* ---*-----------------+-+-+-+-+-+-+-*-*-*-*-----*-----*----\ | *
'* | | | | | | | | | | | | | *
'* .-------------------. --- V V *
'* |D D R E D D V V R V| *
'* |6 7 s 4 5 c s w 0| - + *
'* '-------------------' 5 5 *
'* ":::::::::::::::::::" V V *
'* ":::::LCD::16*2:::::" *
'* ":::::::::::::::::::" *
'* """"""""""""""""""""" *
'* *
'* *
'****************************************************************************
$regfile = "m8def.dat"
$crystal = 8000000 'Internal RC oscillator 8 MHz
$lib "lcd4.lbx"
Config Lcdpin = Pin , Rs = Portb.0 , E = Portb.2 , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7
Config Lcd = 16 * 2
Config Pind.2 = Input
Config Portc.2 = Output
Config Timer0 = Timer , Prescale = 256 '8000000/256=31250 Hz
Config Int0 = Falling 'Interruption on Falling
Stop Timer0
Enable Timer0
Enable Int0
Enable Interrupts
On Timer0 Tikers 'work on timer
On Int0 Infrared 'work on interruption
Dim Tik As Word 'counter of teaks of timer
Dim Byt As Byte 'counter accepted bit
Dim Repeat_flag As Bit 'flag of repetition
Dim Start_flag As Bit 'flag of start condition
Dim Address As Byte 'byte of address
Dim Command As Byte 'byte of command
Dim Address_1 As Byte 'direct byte of address
Dim Command_1 As Byte 'direct byte of command
Dim Address_0 As Byte 'indirect byte of address
Dim Command_0 As Byte 'indirect byte of command
Dim Summa As Word
Cursor Off 'Switch Off cursor
'################################################################################################################
Do 'Main cycle
Cls 'Clean LCD
Lcd Address ; " " ; Command 'Lcd ADDRESS and COMMAND
If Command = 8 Then Portc.2 = 1 'If pressed key "P+" - to include LED
If Command = 240 Then Portc.2 = 0 'If pressed key "P-" - to switch off LED
Waitms 10 'Delay 10 ms
Loop
End 'End of main cycle
'################################################################################################################
Tikers : 'work on timer
Timer0 = 253 '31250/(256-253)=10416,66 Hz (96 us)
Incr Tik
If Tik >= 1200 Then 'if 1200 teaks, have thrown all in source condition
Tik = 0
Repeat_flag = 0
Start_flag = 0
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
Address = 0
Command = 0
Stop Timer0
End If
Return
'################################################################################################################
Infrared : 'work on interruption
Start Timer0
If Tik >= 139 And Tik < 150 Then 'if has happenned from 139 before 150 teaks - "START"
Address = 1 '9ms
Repeat_flag = 0
Start_flag = 1
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
End If
If Tik >= 116 And Tik < 139 Then 'if has happenned from 116 before 138 teaks - "REPETITION"
Address = 2
Repeat_flag = 1
Start_flag = 0
End If
If Tik >= 22 And Tik < 116 And Start_flag = 1 Then 'if has happenned from 22 before 115 teaks - have taken "1"
Incr Byt
If Byt < 9 Then
Shift Address_1 , Left
Address_1 = Address_1 + 1
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
Address_0 = Address_0 + 1
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
Command_1 = Command_1 + 1
End If
If Byt >= 25 Then
Shift Command_0 , Left
Command_0 = Command_0 + 1
End If
End If
If Tik >= 10 And Tik < 22 And Start_flag = 1 Then 'if has happenned from 10 before 21 teaks - have taken "0"
Incr Byt
If Byt < 9 Then
Shift Address_1 , Left
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
End If
If Byt >= 25 Then
Shift Command_0 , Left
End If
End If
Tik = 0
If Byt = 32 Then 'if have taken 4 bytes, check correctness a receiving a command
'if address or command 16-bit, check does not pass
'Summa = Address_0 + Address_1
'If Summa = 255 Then
Address = Address_1 ' Address = Address_1
' Else
' Address = 0
'End If
'Summa = Command_0 + Command_1
'If Summa = 255 Then
Command = Command_1 ' Command = Command_1
' Else
' Command = 0
'End If
Byt = 0
Repeat_flag = 0
Start_flag = 0
Stop Timer0
End If
Return |
(BASCOM-AVR version : 2.0.7.6 , Latest : 2.0.7.7 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Apr 28, 2014 10:14 am Post subject: |
|
|
hi Rick
I did not read your whole post.
i advise to use the timer in 16 bit mode. Then also use the period register. While usually we set up an overflow value which we calculate by subtracting it from the max timer value : 65536-100 for example to overflow after 100 ticks, we can load the 100 directly into the xmega period register. or you can use the old method and load the calculated value into the counter. loading in period has the advantage that you do not need to reload.
I also advise to first play with timers (the samples from samples\xmega) and then put it into action.
if you can not sort it out let me know. _________________ Mark |
|
Back to top |
|
|
rsavas
Joined: 10 Aug 2010 Posts: 197 Location: Baldwin, Ontario
|
Posted: Mon Apr 28, 2014 6:38 pm Post subject: |
|
|
Hi Mark,
Thx, I will spend some more time working out what is going on with the original code and what I need to do in the xmega counter/timer configuration/routines to get it going, before I raise the white flag.
BTW, Hope you are having a nice spring, the daffodils are finally in bloom here, a week late for Easter
Cheers
Rick |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Apr 28, 2014 7:14 pm Post subject: |
|
|
we have an early and warm spring. some days it feels like summer, really amazing. too bad we could not skate this year. _________________ Mark |
|
Back to top |
|
|
rsavas
Joined: 10 Aug 2010 Posts: 197 Location: Baldwin, Ontario
|
Posted: Mon May 05, 2014 4:19 am Post subject: NEC IR decoder using xmega |
|
|
Hi Folks,
Well I finally figured it out, so here it is ...
It seems to be pretty robust and reliable, based on my initial testing (~1 hour). Funny how much effort can go into controlling a LED
I will move on and get the transmitter/remote going and fold this code into my stereo project.
It is rudimentary test code, so still some stuff to sort out, i.e. repeat codes.
Enjoy
Rick
Code: | '*****************************************************************************************************
'* Filename : Get_Ned_ir_xmega_V1.0.BAS *
'* Revision : 1.0 *
'* Function : Program decodes NEC IR protocol *
'* Controller : ATxmega128A1 *
'* References : (AN #157 - Implementation of IR NEC protocol *
'* : http://www.sbprojects.net/projects/ircontrol/nec.php *
'* : http://www.circuitvalley.com/2013/09/nec-protocol-ir-infrared-remote-control.html *
'* : http://techdocs.altium.com/display/ADRR/NEC+Infrared+Transmission+Protocol *
'* Author : Rubashka Vasiliy , Ukraine , 2007 (Thanks!!) *
'* Modify : Rick Savas, for xmega, using counter Tcc1 *
'* Date : 2014-May-04 *
'* Compiler : BASCOM by MCS Electronics *
'* Compiler ver: 2.0.7.6 or higher *
'* Hardware : Sparkfun xmega100 BOB (ATxmega128A1AU) with additional parts *
'* 4x40 Character LCD, Newhaven NHD-0440WH-ATMI-JT# [STN-Blue (Negative)] *
'* 4x40 Character LCD, Newhaven NHD-0440AZ-FL-YBW [Yellow/Green LED Backlight] *
'* PGA2310 *
'* PCA9555 *
'* FT232RL USB virtual UART *
'* 3x Bourns PC12 rotary encocders with switches *
'* 6x spst switches with LED (C&K K5V-BU) *
'* Channel Master Rotator Controller Model 9537 (Antenna Remote) *
'*****************************************************************************************************
'$regfile = "m8def.dat" 'original code MCU
$regfile = "xm128A1def.dat"
$crystal = 32000000
Config Osc = Disabled , 32mhzosc = Enabled '32MHz and 32KHz enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1 'CPU Clock = 8MHz
Config Eeprom = Mapped 'must be used for Xmega EEPROM
'Init and enable the DFLL (Digital Frequency Locked Loop) for automatic run-time calibration of the internal 32MHz Oscillator
Osc_dfllctrl = &B00000000 'The internal 32.768 KHz Oscillator is used for calibration
Set Dfllrc32m_ctrl.0 'Internal RC oscillator 8 MHz
'Config Lcdpin = Pin , Rs = Portb.0 , E = Portb.2 , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7
'Config Lcd = 16 * 2
'this is the customized LCD assembler code to use vport 0 in 4 bit mode, with busy reading
'found in C:\Program Files\MCS Electronics\BASCOM-AVR\LIB
$lib "lcd4e2busy.lib"
'*************
'* Variables *
'*************
Dim Tik As Word : Tik = 0 'counter of teaks of timer
Dim Byt As Byte 'counter accepted bit
Dim Repeat_flag As Bit 'flag of repetition
Dim Start_flag As Bit 'flag of start condition
Dim Address As Byte : Address = 0 'byte of address
Dim Command As Byte : Command = 0 'byte of command
Dim Address_1 As Byte 'direct byte of address
Dim Command_1 As Byte 'direct byte of command
Dim Address_0 As Byte 'indirect byte of address
Dim Command_0 As Byte 'indirect byte of command
Dim Summa As Word
Dim Ir_rx_flag As Bit : Ir_rx_flag = 0
'
Dim Lcd_contrast As Word : Lcd_contrast = 4095 'will use DAC to control LCD contrast (Vo)
Dim Sw_version As String * 3 : Sw_version = "1.0"
'
'--------------------------------Configure Port A-------------------------------
'
Config Porta = Input
Porta_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porta_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'Config Xpin = Porta.0 , Slewrate = DISABLED , Outpull = PULLUP , Sense = FALLING
'setup the DACA using porta.2
Config Daca = Enabled , Io0 = Enabled , Channel = Single , Reference = Int1v , Interval = 64 , Refresh = 64
Start Daca
Daca0 = Lcd_contrast '1 V output on porta.2
'Stop Daca ' to diabled it
'
'--------------------------------Configure Port B-------------------------------
'
'Portb = &H00
Config Portb = Output
Portb_pin0ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin1ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin2ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin3ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin4ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin5ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portb_pin7ctrl = &B10_011_010 ' slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Lcd_led1 Alias Portb.0 'controls backlight LED
Lcd_led2 Alias Portb.1 'controls backlight LED
'Tone_led Alias Portb.1 'controls Tone Defeat LED
Mute_led Alias Portb.3 'controls Mute LED
Tape_mon_led Alias Portb.4 'controls Tape Monitor LED
Tuner_seek_led Alias Portb.5 'controls Tuner Seek LED
Menu_led Alias Portb.6 'controls Menu LED
Power_led Alias Portb.7 'controls Power/Standby LED
'setup the DACA using portb.2
'Config Dacb = Enabled , Io0 = Enabled , Channel = Single , Reference = Avcc , Interval = 64 , Refresh = 64
'
'-------------------------------Configure Port C--------------------------------
'
'Config TWI,PC.0=SDA,PC.1=SCL
Dim Twi_start As Byte ' This Variable is used by the I2C functions
Open "TWIC" For Binary As # 10 ' Pull-Up resistor (4K7) on SDA and SCL to Vcc = 3,3V
'Portc.0 'SDA Pin of Port C (ATXMEGA192A3)
'Portc.1 'SCL Pin of Port C (ATXMEGA192A3)
Config Twic = 100000 '100KHz clock
I2cinit # 10 'set i2c pins to correct state , open collector , pull up activated
'COM Interface to PC via USB using FT232RL ('com1 is located on Port C,PC2=RXD,PC3=TXD)
'TCC0 is used for the UART
Config Com1 = 38400 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8 '38400 for PC application support
'Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "COM1:" For Binary As # 1
'{027}[K Erase Line:
'{027}[2J Erase Display:
Print # 1 , "{013}" ; "{010}" 'send a <cr> & <lf>
Print # 1 , "Get_NEC_IR_xmega_v" ; Sw_version ; ".BAS" 'start up banner
'CONFIG SPI MASTER on Port C
'PGA2311: MSB first, clocked on rising edge,R7..R0,L7..L0
'
' MOSI, PortC.5: Connect to PGA2311.3 (SDI)
' MISO, PortC.6: Not used
' SCK, PortC.7: Connect to PGA2311.6 (SCLK)
' CS, PortC.4: Connect to PGA2311.2 (~CS)
Config Portc.4 = Output
Pga_cs Alias Portc.4
Set Pga_cs 'Slave Select Pin, Connect to PGA2311-2 (CS), active low
Config Spic = Hard , Master = Yes , Mode = 0 , Clockdiv = Clk32 , Data_order = Msb , Ss = None
'Config Spic = Hard , Master = Yes , Mode = 0 , Clockdiv = Clk64 , Data_order = Msb , Ss = None
Open "SPIC" For Binary As # 2
'Portc_pin0ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'Portc_pin1ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'Portc_pin2ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'Portc_pin3ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portc_pin4ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portc_pin5ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portc_pin7ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'
'-------------------------------Configure Port D--------------------------------
'
Config Portd.0 = Output 'Output for PWM
'Use as a ~1KHz square wave source for testing PGA2310
'Config Tcd0 Prescale = 8 , Wgmode = Pwm
' Select Clock Source for timer/counter0
Tcd0_ctrla = &B0000_0010 'DIV2 --> 8MHz/2 = 4MHz
' <--TCD0_CCA->
' +-----------+ +
' | | |
'------+ +----------+
'
' <--------------------->
' Period = 1.02mSec(977Hz)
'
'Enable PWM Output
Tcd0_ctrlb = &B0001_0_011
' ^ ^
' ^ Waveform Generation Mode = Single Slope PWM
' Enable OD0A ---> PinD.0 for PWM Output
'SET Resolution of PWM (min. = &H0003 ...... max. = &HFFFF)
'Tcd0_per = &HFFFF 'Set Period = FFFF = 65535 --> 65535/4MHz = 16.38mSec(61Hz)
Tcd0_per = &H0FFF 'Set Period = FFF = 4095 --> 4095/4MHz = 1.02mSec(977Hz)
'Change TCD0_CCA (Duty Cycle)
Tcd0_cca = &H07FF '2047/4MHz = 0.512mSec
'Tcd0_cca = 32767 '32767/4MHz = 8.19mSec
'Tcd0_cca = 20000 '20000/4MHz = 5ms
'Tcd0_cca = 10000 '10000/4MHz = 2.5mSec
'
'--------------------------------Configure Port E-------------------------------
'
'Porte_dir = &B1011_1011 'If DIRn is written to one, pin is configured as an output pin.
Porte_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porte_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'
'--------------------------------Configure Port F-------------------------------
'Portf_dir = &B0000_0000 'If DIRn is written to one, pin is configured as an output pin.
Config Portf = Input
Sw_enc1 Alias Pinf.0 'switch part of encoder1,toggles between Analog or Digital selection of channels
Sw_enc2 Alias Pinf.1 'switch part of encoder2,toggles between Volume or Balance mode
Sw_enc3 Alias Pinf.2 'switch part of encoder3,used for USB & Tuner
Sw_power_on Alias Pinf.3 'Power on/standby switch
Sw_mute Alias Pinf.4 'Mute Switch
Sw_tape_mon Alias Pinf.5 'Tape monitor switch
Sw_tuner_seek Alias Pinf.6 'Tuner_seek switch
Sw_menu Alias Pinf.7 'Menu switch
Portf_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portf_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'------------------------------Configure Port H---------------------------------
Config Vport0 = H 'Real Port Direction
Ddrh = &B1111_1111
Vport0_dir = &B1111_1111 'Virtual Port 0 = OUTPUT
'Porth_dir = &B1011_1011 'If DIRn is written to one, pin is configured as an output pin.
'---------------------------------LCD Setup-------------------------------------
'use lcd4e2busy.lib and define port H as vport 0. The lib routines will control the port pin directions thereafter
Porth_pin0ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin1ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin2ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin3ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin4ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin5ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin6ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Porth_pin7ctrl = &B10_011_010 'slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Config Lcd = 40 * 2 'Config Lcd = 40 * 2(update 2.0.7.3 :Both are valid, but the * will become invalid in the future.
Dim ___lcde As Byte 'use for two Enable module type,0 = Top 2 rows, 1 = Bottom 2 rows
Initlcd 'same as asm "Rcall _init_lcd"
'Waitms 100
___lcde = 0 'select the upper 2 line controller 'clear the LCD display
Cursor Off Noblink 'hide cursor
Cls
___lcde = 1 'select the lower 2 line controller 'clear the LCD display
Cursor Off Noblink 'hide cursor
Cls
___lcde = 0
'------------------------------Configure Port J---------------------------------
'PortJ_dir = &B0000_1111 'If DIRn is written to one, pin is configured as an output pin.
Config Portj = Output
18v_sw_c Alias Portj.0 'controls Analog Voltage supply
Rf_sw1 Alias Portj.1 'controls RF Band switch diode
Rf_sw2 Alias Portj.2 'controls RF Band switch diode
Ir_pin Alias Pinj.6 'input from TSOP4836
Config Pinj.6 = Input
Portj_pin0ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin1ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin2ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin3ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin4ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin5ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin6ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portj_pin7ctrl = &B00_011_010 ' no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
'^ ^
'^ Reaction on falling edge
'^
'Enable Pullup
Portj_int0mask = &B0100_0000 'include PIN6 in INT0 Mask
Portj_intctrl = &B0000_00_11
'^^
'||-High Level INT0 Interrupt
'Config Int0 = Falling 'Interruption on Falling
'On Int0 Infrared 'work on interruption
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Configure Port K; Used for Bourns encoders & PGA MUTE & ZCEN'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'PortK_dir = &B1100_0000 'If DIRn is written to one, pin is configured as an output pin.
Config Portk = Input
Config Pink.6 = Output 'PGA2311 Mute, active Low
Pga2311_mute Alias Portk.6
Set Pga2311_mute
Config Pink.7 = Output 'PGA2311 ZCEN, active High
Pga2311_zen Alias Portk.7
Reset Pga2311_zen
Portk_pin0ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin1ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin2ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin3ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin4ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin5ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin6ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Portk_pin7ctrl = &B00_011_010 'no slew, no invert, Totempole, Pull-up (on input),Sense falling edge
Lcd_led1 = 0 'controls backlight LED
Lcd_led2 = 1
Mute_led = 1 'controls Mute LED
Tape_mon_led = 1 'controls Tape Monitor LED
Tuner_seek_led = 1 'controls Tuner Seek LED
Menu_led = 1 'controls Menu LED
Power_led = 1
Waitms 500
Mute_led = 0 'controls Mute LED
Tape_mon_led = 0 'controls Tape Monitor LED
Tuner_seek_led = 0 'controls Tuner Seek LED
Menu_led = 0 'controls Menu LED
Power_led = 0
___lcde = 0 : Locate 1 , 1
Lcd "Get_NEC_IR_xmega_v" ; Sw_version ; ".bas " 'start up banner
___lcde = 1 : Locate 1 , 1
'----------------------------------Interrupts-----------------------------------
Config Priority = Static , Vector = Application , Hi = Enabled 'Hi = Enabled
On Portj_int0 Infrared 'On pin change, vector to interrupt service routine
Enable Portj_int0 , Hi 'Enable this Interrupt as Hi Level Interrupt
On Tcc1_ovf Tikers 'vector to tcc1 overflow interupt
Enable Tcc1_ovf , Hi 'enable it
Tcc1_ctrlb = &B0000_0_000 'WGMode=000, normal operation
Tcc1_ctrlfset = &B0000_11_00 'cmd,hard reset,dir up
Tcc1_per = 320 'The Counter period is changed by writing a new TOP value to the Period Register.
Tcc1_ctrlb = &B0000_0_000 'WGMode=000
Tcc1_intctrla = &B0000_00_11 'Enable high level interrupt
' || <- These bits enable the Timer Overflow/Underflow Interrupt and select the interrupt level as
' described in Interrupts and Programmable Multi-level Interrupt Controller on page 123.
'Tcc1_ctrla = &B0000_0001 'DIV1 --> 32MHz(31.25ns) clock period,starts the timer running
Enable Interrupts
'################################################################################################################
Do 'Main cycle
If Ir_rx_flag = 1 Then
Ir_rx_flag = 0
Locate 1 , 1 : Lcd Spc(10) : Locate 1 , 1 'clear out last entry
Lcd Address ; " " ; Command 'display ADDRESS and COMMAND in decimal
If Address = 53 Then 'use LED's to indicate some received commands
Select Case Command 'sent from channel master remote
Case 56 : Toggle Power_led
Case 128 : Toggle Mute_led
Case 64 : Toggle Tape_mon_led
Case 192 : Toggle Tuner_seek_led
Case 32 : Toggle Menu_led
Case 8 : Toggle Lcd_led1
Case 136 : Toggle Lcd_led2
End Select
End If
'Waitms 500
End If
Loop
End 'End of main cycle
'################################################################################################################
Tikers : 'this routine is called when the tcc1 counter oveflows = tcc1_per value
'Menu_led = 1 'DEBUG
Tcc1_intflags.0 = 1 'Writing a one to this flag's bit location will clear the overflow flag
Incr Tik
If Tik >= 1500 Then '31.25ns * 320 * 1500 = 15ms expired, stop timer, as error condition
Tcc1_ctrla = &B0000_0000 'Stop Tcc1
'Tcc1_ctrlfset = &B0000_11_01 'cmd,hard reset,dir up
Tik = 0 'reset all variables
Repeat_flag = 0
Start_flag = 0
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
Address = 0
Command = 0
Ir_rx_flag = 0
'Waitms 250
'Menu_led = 0 'DEBUG
End If
Return
'################################################################################################################
Infrared : 'Portj_int0 pin change, interrupt service routine
'Power_led = 1 'DEBUG
Tcc1_ctrla = &B0000_0001 'DIV1 --> 32MHz=31.25ns 'Start Tcc1
If Tik >= 1210 And Tik < 1490 Then '"START" condition
Address = 1 '9 + 4.5 = 13.5ms nominal
Repeat_flag = 0 '
Start_flag = 1
Address_1 = 0
Command_1 = 0
Address_0 = 0
Command_0 = 0
End If
If Tik >= 1010 And Tik < 1210 Then '"REPETITION" condition
Address = 2 '9 + 2.25 = 11.25ms nominal
Repeat_flag = 1 '
Start_flag = 0
End If
If Tik >= 200 And Tik < 1010 And Start_flag = 1 Then '"1" condition
Incr Byt '2.25ms typical
'
If Byt < 9 Then
Shift Address_1 , Left
Address_1 = Address_1 + 1
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
Address_0 = Address_0 + 1
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
Command_1 = Command_1 + 1
End If
If Byt >= 25 Then
Shift Command_0 , Left
Command_0 = Command_0 + 1
End If
End If
If Tik >= 100 And Tik < 200 And Start_flag = 1 Then '"0" condition
Incr Byt '1.12ms typical
'
If Byt < 9 Then
Shift Address_1 , Left
End If
If Byt >= 9 And Byt < 17 Then
Shift Address_0 , Left
End If
If Byt >= 17 And Byt < 25 Then
Shift Command_1 , Left
End If
If Byt >= 25 Then
Shift Command_0 , Left
End If
End If
Tik = 0
If Byt = 32 Then 'if have taken 4 bytes, check correctness a receiving a command
'if address or command 16-bit, check does not pass
'Summa = Address_0 + Address_1
'If Summa = 255 Then
Address = Address_1 ' Address = Address_1
' Else
' Address = 0
'End If
'Summa = Command_0 + Command_1
'If Summa = 255 Then
Command = Command_1 ' Command = Command_1
' Else
' Command = 0
'End If
Byt = 0
Repeat_flag = 0
Start_flag = 0
Ir_rx_flag = 1
Tcc1_ctrla = &B0000_0000 'Stop Tcc1
Tcc1_intflags.0 = 1 'Writing a one to this flag's bit location will clear the overflow flag
End If
'Power_led = 0 'DEBUG
Return |
|
|
Back to top |
|
|
|
|
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 You cannot attach files in this forum You cannot download files in this forum
|
|