News:

Let's find out together what makes a PIC Tick!

Main Menu

Where am I going wrong?

Started by shantanu@india, Jul 03, 2021, 12:49 PM

Previous topic - Next topic

shantanu@india

Hi all,
Nice to see many old friends.
This small piece of code is not behaving itself , the GPIO.0 pin stays low forever.
Any ideas?

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 12F675
Xtal=4.00
Config FOSC_INTRCIO, WDTE_OFF, PWRTE_ON, MCLRE_OFF, BOREN_ON, CP_OFF, CPD_OFF
Reminders =0
Warnings=0
Declare All_Digital true

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
' 30.6.21 This generates 4 frequencies....100/1000/1500/3000 RPM
'100 RPM = (60/100)*1000000 usec = 600000 usec = 600 msec
'1000 RPM = 60000 usec
'1500 RPM = (60/1500)*1000000 usec = 600000/15 = 40000 usec
'3000 RPM = 20000 usec
'****************************************************************
  'variables
 Dim    freq_count  As Word 
'******************************************************************
'hardware details
Symbol  freq_out  GPIO.0
Symbol  set1      GPIO.1    '100 rpm
Symbol  set2      GPIO.2    '1000 rpm 
Symbol  set3      GPIO.3    '1500 rpm
Symbol  set4      GPIO.4    '3000 rpm
Symbol  set5      GPIO.5
'******************************************************************* 

'initialization  of registers


    GPIO=0   
    TRISIO=%111110     
   
'**********************************************************************   
 
run_mode:     
   
    While 1 = 1
        freq_out=1
           
        If set1=1 Then             
            DelayMS 300           
        ElseIf set2=1 Then             
            DelayUS 30000           
        ElseIf set3=1 Then           
            DelayUS 20000             
        ElseIf set4=1 Then             
            DelayUS 10000           
        EndIf
       
        freq_out=0
        If set1=1 Then             
            DelayMS 300           
        ElseIf set2=1 Then             
            DelayUS 30000           
        ElseIf set3=1 Then           
            DelayUS 20000             
        ElseIf set4=1 Then             
            DelayUS 10000           
        EndIf
       
    Wend
           
       
Regards
Shantanu

Craig

Hi Shantanu

Seems silly but, have you tried a different pin to see if pin 0 isn't perhaps damaged.
regards
Craig

top204

#2
I've just tried your code in Isis and it is working. I have altered the code's layout so I could follow it a bit better:

    Device = 12F675
    Declare Xtal = 4
   
    Config FOSC_INTRCIO, WDTE_OFF, PWRTE_ON, MCLRE_OFF, BOREN_ON, CP_OFF, CPD_OFF
'
' 30.6.21 This generates 4 frequencies....100/1000/1500/3000 RPM
' 100 RPM  =  (60/100)*1000000 usec  =  600000 usec  =  600 msec
' 1000 RPM  =  60000 usec
' 1500 RPM  =  (60/1500)*1000000 usec  =  600000/15  =  40000 usec
' 3000 RPM  =  20000 usec
'
' Create variables
'
    Dim wFreqCount As Word
'
' Create pin alias names
'
    Symbol Freq_Out_Pin = GPIO.0
    Symbol Set1_Pin     = GPIO.1    ' 100 rpm
    Symbol Set2_Pin     = GPIO.2    ' 1000 rpm
    Symbol Set3_Pin     = GPIO.3    ' 1500 rpm
    Symbol Set4_Pin     = GPIO.4    ' 3000 rpm
    Symbol Set5_Pin     = GPIO.5
 
'--------------------------------------------------------------------
' The main program starts here
'
Main: 
    Setup()
   
    Do
        Freq_Out_Pin = 1          
        If Set1_Pin = 1 Then            
            DelayMS 300          
        ElseIf Set2_Pin = 1 Then            
            DelayUS 30000          
        ElseIf Set3_Pin = 1 Then          
            DelayUS 20000            
        ElseIf Set4_Pin = 1 Then            
            DelayUS 10000          
        EndIf
      
        Freq_Out_Pin = 0
        If Set1_Pin = 1 Then            
            DelayMS 300          
        ElseIf Set2_Pin = 1 Then            
            DelayUS 30000          
        ElseIf Set3_Pin = 1 Then          
            DelayUS 20000            
        ElseIf Set4_Pin = 1 Then            
            DelayUS 10000          
        EndIf     
    Loop
   
'-------------------------------------------------------------------- 
' Initialisation of registers
'
Proc Setup()
    Low Freq_Out_Pin
    Input Set1_Pin 
    Input Set2_Pin     
    Input Set3_Pin     
    Input Set4_Pin
    Input Set5_Pin
EndProc

The first thing to do is make sure your device is running. Create a loop that toggles the LED with a delay inside it so the LED can be seen illuminating and extinguishing:

Main: 
    Setup()
   
    Do
        Toggle Freq_Out_Pin
        DelayMS 500
    Loop

Once the LED is seen toggling at half a second, you know the device is running OK.

As can be seen by my code alteration, I always use the text "_Pin" following alias names that are aliased to a device's pin. This make following the code flow a lot easier because you can see the function of the alias name.

shantanu@india

Regards
Shantanu

shantanu@india

Craig,
I tried 2/3 PICs with the same result. There must be some simple cause.
I'll try Les's code but I checked the compiled assemblycode and could not find anything wrong.
Regards
Shantanu

trastikata

#5
Helo Shantanu,

instead of "freq_out=1" and "freq_out=0" use "High freq_out" and "Low freq_out".

The first will generate this code:
F1_000043 equ $ ; in [TEST.BAS] freq_out=1
    bsf GPIO,0
....
F1_000055 equ $ ; in [TEST.BAS] freq_out = 0
    bcf GPIO,0

and the second this:
F1_000043 equ $ ; in [TEST.BAS] high freq_out
    bsf STATUS,5
ram_bank = 1
    bcf TRISB,0
    bcf STATUS,5
ram_bank = 0
    bsf GPIO,0
....
F1_000055 equ $ ; in [TEST.BAS] low freq_out
    bsf STATUS,5
ram_bank = 1
    bcf TRISB,0
    bcf STATUS,5
ram_bank = 0
    bcf GPIO,0


Regards

top204

#6
Try the LED toggle loop code to verify the microcontroller is actually working.

There is no need for the High and Low commands if the pin has already been set as an output. The High and Low commands set the pin as an output before changing its condition, so if the pin is already an output, it wastes code space and time because the TRIS SFR is altering everytime, which is not needed if the pin is always an output within the program.

With devices that have LAT SFRs, it is better to use Set or Clear or PinSet or PinClear because they will automatically use the pin's LAT SFR, instead of the PORT, so it stops the pin querk in the devices, but standard 14-bit core devices do not have LAT SFRs, so any way of changing the pin's state is the same once it is made an output.

shantanu@india

It turned out to be a short between GPIO. 0 and ground. :-X
Sorry to have bothered you Les with an inane issue, but your posts at least helped to brush off the rust from my knowledge about the little 12F's with whom I haven't worked in a while.
Regards
Shantanu

shantanu@india

Another slip up on my part.....plain forgot to put 'Set_OSCCAL' at the begining of the code!!
Without that the internal oscillator frequency was higher by 20%!!
Instead of 1500 RPM the PIC was generating frequency equivalent to 1800 RPM.
Regards
Shantanu