News:

;) This forum is the property of Proton software developers

Main Menu

CODIGO NEC FOR CONTROL TX

Started by superpro, Jul 22, 2022, 01:59 AM

Previous topic - Next topic

Pepe

#40
demo isis

Teo


Pepe

this is the program but it can't be simulated in Proteus

Device = 16F1826

$define PWM_Pulse_On() PinOutput TX_Pin
$define PWM_Pulse_Off() PinInput TX_Pin

Declare Optimiser_Level = 3
Declare Create_Coff On
Declare Bootloader off
Declare Create_Coff On
Declare Watchdog = Off
Declare Xtal = 32                               ' Tell the compiler what frequency the device will be operating at

'Declare HPWM1_Pin = PORTB.3                     ' Tell the compiler what pin is used for the CCP1 peripheral

Symbol GIEH = INTCON.7   ' Global Interrupt Enable
Symbol GIEL = INTCON.6   ' Peripheral Interrupt Enable
Symbol TMR0IE = INTCON.5 ' TMR0 Overflow Interrupt Enable
Symbol INTE = INTCON.4 
Symbol IOCIE = INTCON.3  '
Symbol TMR0IF = INTCON.2 ' TMR0 Overflow Interrupt Flag
Symbol INTF = INTCON.1 
Symbol IOCIF = INTCON.0 '
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

;Device = 16F1826

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_ON, STVREN_OFF, LVP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Symbol    PERIOD       1000.0       ; 100 us
Symbol    Xtl          8000000      ; frequency - 8MHz
Symbol    ICLK         (Xtl/4)      ; crystal is divided by four
Symbol    SCALE        8            ; prescale by 8 - check for overflow!

Symbol    PRELOAD         255-(PERIOD*ICLK/SCALE)/1000000

' --- Configurar oscilador interno ---
IOCBN=$00
IOCBP=$80
IOCBF=$00                       
OSCCON = $70 ' Oscilador interno a 32 MHz
TRISA=$FF
TRISB= $80

Symbol TX_Pin = PORTB.3                         ' The CCP1 peripheral's pin to use for the IR LED

Symbol device_1= 1
Symbol device_2= 2

'
' Create some variables
'
    Dim RC5_bSystem  As Byte                        ' Holds the system value
    Dim RC5_bCommand As Byte                        ' Holds the command value
    Dim RC5_tToggle  As Bit                         ' Holds the toggle bit
    Dim bBitIndex As Byte
    Dim devices As Bit
    Dim timer As Word
    Dim timer1 As Word = 300
    Dim timer2 As Word =0
    Dim flg As Bit  = 1
    Dim vez As Byte = 0
    Symbol led1 = PORTB.1
    Symbol led2 = PORTB.2
   
   On_Hardware_Interrupt GoTo Isr
   
   TMR0 = PRELOAD
   OPTION_REG = 4
   led1=0
   led2=0
   TMR0IE = 1
   IOCIE = 1
   GIEL = 1
   GIEH = 1
   

'-------------------------------------------------------------------------------------------------------
' The main program starts here
' Transmit the system and command values using the RC5 infrared protocol
'

 EPWM_56KHz()

 Do
   
   If timer2 = 0 Then Sleep
   
   If devices = 0 Then
                        RC5_bSystem = device_1
                       
                        If timer1 = 0 Then
                                           timer1 = 300
                                           If vez > 0 Then
                                                           Dec vez
                                                           Toggle led1
                                                      Else
                                                           led1 = 1
                                           EndIf
                        EndIf
                                           
                        led2=0
                   Else
                        RC5_bSystem = device_2
                       
                        led1=0
                       
                        If timer1 = 0 Then
                                           timer1 = 300
                                           If vez > 0 Then
                                                           Dec vez
                                                           Toggle led2
                                                      Else
                                                           led2 = 1
                                           EndIf
                        EndIf     
                       
    EndIf
   
    Select Case PORTA

    Case 1
     RC5_bCommand = 1
    Case 2
     RC5_bCommand = 2
    Case 4
     RC5_bCommand = 3
    Case 8
     RC5_bCommand = 4
    Case $10
     RC5_bCommand = 5
    Case $20
     RC5_bCommand = 6
    Case $40
     RC5_bCommand = 7
    Case $80
     RC5_bCommand = 8
    Case Else
     RC5_bCommand = 0
    End Select
   
    If RC5_bCommand <> 0 Then
     
       RC5_SendOne()                               ' Send the first Start bit
       RC5_SendOne()                               ' Send the second Start bit
'
' Send the toggle bit
'
       If RC5_tToggle = 0 Then                     ' Is the toggle bit clear?
           RC5_SendZero()                          ' Yes. So send a logic zero pulse
   Else                                        ' Otherwise... The bit is set 
           RC5_SendOne()                           ' So. Send a logic one pulse
       EndIf
'
' Send the 5-bit System value, MSB first
'
       For bBitIndex = 4 DownTo 0                  ' Create a loop for the 5 system bits
        If GetBit(RC5_bSystem , bBitIndex) = 0 Then  ' Is the bit to send clear?
            RC5_SendZero()                      ' Yes. So send a logic zero pulse
        Else                                    ' Otherwise... The bit is set
            RC5_SendOne()                       ' So. Send a logic one pulse
        EndIf
       Next
'
' Send the 6-bit Command value, MSB first
'
       For bBitIndex = 5 DownTo 0                  ' Create a loop for the 6 command bits   
        If GetBit(RC5_bCommand, bBitIndex) = 0 Then ' Is the bit to send clear?
            RC5_SendZero()                      ' Yes. So send a logic zero pulse
        Else                                    ' Otherwise... The bit is set
            RC5_SendOne()                       ' So. Send a logic one pulse
        EndIf
       Next
       
       PWM_Pulse_Off()                             ' Make sure the 56 KHz modulation is disabled
   
       DelayMS 300
       
       Toggle  RC5_tToggle

      Else

       RC5_tToggle  =0
 
   End If
 
 Loop                                            ' Do it forever
 
Isr:
 Context Save

 If TMR0IF = 1 Then
                   
                   TMR0 = PRELOAD
                   
                   If timer1  > 0 Then Dec timer1
                   If timer2  > 0 Then Dec timer2
                 
                   If PORTB.7 = 1 Then
                                       If timer < 2000 Then Inc timer
                                       flg = 0
                                  Else
                                       If flg = 0 Then
                                                       flg = 1
                                                       If timer < 2000 Then
                                                                            vez = 5
                                                                       Else
                                                                            Toggle devices
                                                                            vez = 0
                                                       EndIf
                                       EndIf
                                       timer = 0     
                   EndIf
                 
                   TMR0IF = 0
 EndIf

 If IOCIF = 1 Then
                  IOCBF = 0
                  IOCIF = 0
                  timer2 = 10000
 EndIf
 
                                 
 Context Restore                 
'------------------------------------------------------------------------------
' Send a two-phase pulse for a logic 0
' Input     : None
' Output    : None
' Notes     : The logic sequence is High to Low
'
Proc RC5_SendZero()
    PWM_Pulse_On()                           ' Enable the 38 KHz modulation
    DelayUS 887                                 ' Delay for the required time
    PWM_Pulse_Off()                             ' Disable the 38 KHz modulation
    DelayUS 884                                 ' Delay for the required time
EndProc

'------------------------------------------------------------------------------
' Send a two-phase pulse for a logic 1
' Input     : None
' Output    : None
' Notes     : The logic sequence is Low to High
'
Proc RC5_SendOne()
    PWM_Pulse_Off()                             ' Disable the 38 KHz modulation
    DelayUS 887                                 ' Delay for the required time
    PWM_Pulse_On()                            ' Enable the 38 KHz modulation
    DelayUS 884                                 ' Delay for the required time
EndProc


'-----------------------------------------------------------
' Setup the EPWM peripheral on a PIC16F1826 device
' Input     : None
' Output    : None
' Notes     : Uses Timer2, that is set for 18uS (approx 55.55KHz)
'           : On a device operating at 32MHz.
'           : PWM reolution is 6-bits, and duty cycle is set for approx 50%
'
Proc EPWM_56KHz()
    PWM_Pulse_Off() 
    PR2 = 35                ' Set Timer2 period
    TMR2 = 0
    T2CON = %00000101       ' Setup Timer2
    CCP1CON = $0C   
    ECCP1AS = $00
    PWM1CON = $80
    PSTR1CON = $01
    CCPR1H = 0
    CCPR1L = 18
    RC5_tToggle = 0                             ' Reset the toggle state

EndProc


Teo

Thank you very,very, very much !
Teo