News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

PIC16F877A SHOUT conflict with PORT C ??-advice welkom

Started by basiclover, Jun 07, 2022, 12:45 PM

Previous topic - Next topic

basiclover

trying to generate a 0.2ms pulse on PORTC.1, by 2 methods
this doesn't work, no pulse at all.Don't understand why
''****************************************************************
'*  Name    : 16F877A SPI simple TEST.BAS                         *                       
'*  Author  : basiclover                                         *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]  *
'*          : All Rights Reserved                                *
'*  Date    : 6/3/2022                                           *
'*  Note    :                                                    *                    
'*          : ver 1                                              *
'*                                                               *
'*****************************************************************

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

'Device = 16F877A

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF

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

'--------------------------------------------------------------------------------------------------------------
'
    Device = 16F877A                             ' Tell the compiler what device is being compiled for
    Declare Xtal = 20                           ' changed 10 to 20-Tell the compiler what speed the device will be operating at
'
    Declare Float_Display_Type = Fast           ' Use the faster, more accurate, but larger, floating point display library routine
'
' Setup the alphanumeric LCD
    Declare LCD_Type = 0       'HITACHI HD44780 controller
    Declare LCD_DTPin = PORTB.4  '4 pin config, data on 4 PORT B top pins(PortB4 to B7)
    Declare LCD_RSPin = PORTB.3
    Declare LCD_ENPin = PORTB.2
    Declare LCD_Lines = 4
   
'configure SPI
    Dim MyDataPin As Pin    ' Holds the pin to use for the SPI data line
    Dim MyClockPin As Pin   ' Holds the pin to use for the SPI clock line
    Dim MyCSPin As Pin ' Holds the pin to use for the SPI CS line
    Dim FQ_UD As Pin
    MyDataPin=PORTC.5
   
    MyClockPin = PORTC.3
    MyCSPin = PORTC.4
    FQ_UD= PORTC.1
   
    Main:
    FQ_UD=0
    Do
    SHOut MyDataPin,MyClockPin,1,[%01111001,8]    'mode "1"=shift out most significant bit first
    FQ_UD=1
    DelayMS 0.2
    FQ_UD=0
    DelayMS 0.2
    FQ_UD=1
    Loop


but this method works

''****************************************************************
'*  Name    : 16F877A SPI simple TEST.BAS                        *                       
'*  Author  : basiclover                                         *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]  *
'*          : All Rights Reserved                                *
'*  Date    : 6/7/2022                                           *
'*  Note    :                                                    *                    
'*          : ver 1                                              *
'*                                                               *
'*****************************************************************

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

'Device = 16F877A

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF

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

'--------------------------------------------------------------------------------------------------------------
'
    Device = 16F877A                             ' Tell the compiler what device is being compiled for
    Declare Xtal = 20                           ' changed 10 to 20-Tell the compiler what speed the device will be operating at
'
    Declare Float_Display_Type = Fast           ' Use the faster, more accurate, but larger, floating point display library routine
'
' Setup the alphanumeric LCD
    Declare LCD_Type = 0       'HITACHI HD44780 controller
    Declare LCD_DTPin = PORTB.4  '4 pin config, data on 4 PORT B top pins(PortB4 to B7)
    Declare LCD_RSPin = PORTB.3
    Declare LCD_ENPin = PORTB.2
    Declare LCD_Lines = 4
   
'configure SPI
    Dim MyDataPin As Pin    ' Holds the pin to use for the SPI data line
    Dim MyClockPin As Pin   ' Holds the pin to use for the SPI clock line
    Dim MyCSPin As Pin ' Holds the pin to use for the SPI CS line
    MyDataPin=PORTC.5
    MyClockPin = PORTC.3
    MyCSPin = PORTC.4
   
    Main:
    Cls
    Low PORTC.1
    Do
    SHOut MyDataPin,MyClockPin,1,[%01111001,8]    'mode "1"=shift out most significant bit first
    High PORTC.1
    DelayMS 0.2
    Low PORTC.1
    DelayMS 0.2
    High PORTC.1
    Loop

charliecoutas

Hi basiclover

Your DelayMS 0.2 isn't doing what you expect. This is equivalent to DelayMS 0. If my memory serves me still, i think this generates a delay of forever, it never ends (but my memory isn't so good these days). Strictly speaking the compiler should flag that as an error.

Try DelayUS 200 which is what you want, 0.2mS

Charlie

Pepe





 
probe this


Main:
    PinLow FQUD
    Do
    SHOut MyDataPin,MyClockPin,1,[%01111001,8]    'mode "1"=shift out most significant bit first
    PinHigh FQUD
    DelayMS 2
    PinLow FQUD
    DelayMS 2
    PinHigh FQUD
    Loop

basiclover

thanks pepe, that works
but I still would like that somebody tels me wy my firdt method is wrong

top204

The delays work with integer values, so 0.2 is seen as 0 because the floating point value is rounded down to 0. And a zero is not valid for DelayMs and will give strange results with certain device families because it calls a library subroutine for the delay, and it cannot waste time with too many checks of the value sent to it to give a delay of the required time, "in time". :-)

For delays below 1ms, use the DelayUs command, because 0.2ms is 200us so a DelayUs 200 command will do it.

I'll add a reminder message if the delay commands see floating point constants or variables as the parameter, just to be on the safe side. :-)


tumbleweed

#5
This part here:
    Dim MyDataPin As Pin    ' Holds the pin to use for the SPI data line
    Dim MyClockPin As Pin   ' Holds the pin to use for the SPI clock line
    Dim MyCSPin As Pin ' Holds the pin to use for the SPI CS line
    Dim FQ_UD As Pin
    MyDataPin=PORTC.5
   
    MyClockPin = PORTC.3
    MyCSPin = PORTC.4
    FQ_UD= PORTC.1
Is not doing what you want. It's declaring those as just regular variables, and statements like 'MyDataPin = PORTC.5' are reading the pin state and setting the variable. There's no output to the actual IO pins.

To define those symbols as port pins use:
    Dim MyDataPin As PORTC.5
    Dim MyClockPin As PORTC.3
    Dim MyCSPin As PORTC.4
    Dim FQ_UD As PORTC.1

You'll have to set them as outputs, and then you'll get the IO pins toggling.


basiclover

sorry to bother again LES, CHARLIE and TUMBLEWEED
I tried all your configuration settings and changed DelayMs 0.2 into Delayus 200
Although Shout works fine I can't get a pulse on PORTC.1
I get it only with the suggestion of PEPE even with DelayMs 0.2(using PinHigh and PinLow)
Mystery!!

charliecoutas

Try adding

TRISC.1 = 0

This will make sure that C.1 is an output.

basiclover


John Lawton

According to the datasheet, PORTC is shared with a number of peripherals, PORTC.1 is shared with Compare2 output/PWM2 outputs so make sure that the PWM peripheral on that pin is disabled.

tumbleweed

#10
The 'Pin' variable type is special... there are some operations that are valid and some that require the 'old' method of declaring the pin. Sometimes you can mix the two but not always.

    '---------------------------
    ' method 1 - using 'As Pin'
    '---------------------------
    Dim FQ_UD As Pin
    FQ_UD = PORTC.1

    ' these are valid for a 'Pin' type
    PinHigh FQ_UD
    PinLow FQ_UD
    High FQ_UD
    Low FQ_UD
    ' but these are not... the output will not change
    FQ_UD = 1
    FQ_UD = 0

    '---------------------------------------
    ' method 2 - using 'As PORTx.n' or 'Symbol'
    '---------------------------------------
    'Dim FQ_UDC1 As PORTC.1   ' can use dim or symbol to define the pin...
    Symbol FQ_UDC1 = PORTC.1

    Output FQ_UDC1          ' make PORTC.1 an output (clears TRISC.1)

    ' these are now valid ways to change the output
    FQ_UDC1 = 1
    FQ_UDC1 = 0

    ' these should also work with method 2
    High FQ_UDC1
    Low FQ_UDC1
    PinHigh FQ_UDC1
    PinLow FQ_UDC1


If you're concerned with speed or code size, I'd use method #2 (and avoid 'High/PinHigh', etc)

basiclover

Thanks a lot TUMBLEWEED
I tested  your method 2 with and without "Output FQ_UDC1".Both work.Forgot to change DelayMs 0.2 into DelayUS 200, but still works
For now my problem is solved.
You say "this will work" and "This will not work". Is it by experience or are there explanations for that?
I'm still frustrated that a series of individual statements which seems correct according to the manual work in some instances and not in others although the whole thing compiles but don't yield the expected result
thanks again