beginners help with a simple program (on a 16f690 or 12f863)

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

beginners help with a simple program (on a 16f690 or 12f863)

Postby QuantumDuck » Wed Oct 16, 2019 5:36 pm

Hi

Haven't touched a pic in nearly ten years and and was only and amateur back then, have forgotten everything.

Trying to make simple control program for a step motor. (making this https://www.thingiverse.com/thing:159981)

basically just a running light program with adjustable speed, something like


gp0 analog in ; for adjusting speed
gp1 to 4 digital out

gp1 to 4 = 1 ; 1 low, 0 high
main
gp4 0
gp1 1
goto wait
gp1 0
gp2 1
goto wait
gp2 0
gp3 1
goto wait
gp3 0
gp4 1
goto wait

goto main

wait
read gp0
set wait time
sleep
return

Can anyone direct me to some sample code or similar program with comments an amateur can understand? or if someone wants to throw together something basic i can build on? (using mplab 8.92 and pic kit 2 with demo board)
QuantumDuck
 
Posts: 3
Joined: Wed Oct 16, 2019 5:00 pm

Re: beginners help with a simple program (on a 16f690 or 12f

Postby ric » Wed Oct 16, 2019 8:20 pm

If you are using MPLAB8.92, then there is a basic template for your PICs at:
C:\Program Files (x86)\Microchip\MPASM Suite\Template\Code\12F683TEMP.ASM
(I assume "12F863" was a typo, there's no such device)
or
C:\Program Files (x86)\Microchip\MPASM Suite\Template\Code\16F690TEMP.ASM

Will that skeleton get you started?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 509
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: beginners help with a simple program (on a 16f690 or 12f

Postby QuantumDuck » Fri Oct 18, 2019 4:03 pm

I've made some progress

Got the led blinking in order

Code: Select all
#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF)
org 0
Start:
   bsf    STATUS,RP0   ;banksel option_reg better?
   movlw   b'00000111'  ;init timer0 med max prescale
   movwf   OPTION_REG
   BSF STATUS,RP0 ;select Register Page 1
   movlw b'00000000'
   movwf TRISC
   BCF STATUS,RP0 ;back to Register Page 0
   goto mainloop

delay1
   btfss   INTCON,T0IF
   goto    $-1
   bcf      INTCON,T0IF
   return

mainloop:
   BSF PORTC,0 ;turn on LED C0
   call delay1
   BSF PORTC,1
   call delay1
   BSF PORTC,2
   call delay1
   BSF PORTC,3
   call delay1
   
       GOTO mainloop
end



How do i change the delay using a potentiometer? I'm guessing setting the timer0 to a lower value and having a "delaymain" call delay1 a certain number of times based on the analog value
Also, why do this code only light 1 led at the time, theres is probably an obvious reason why i dont need to write BCF PORTC,3 after each to turn off the led
QuantumDuck
 
Posts: 3
Joined: Wed Oct 16, 2019 5:00 pm

Re: beginners help with a simple program (on a 16f690 or 12f

Postby ric » Fri Oct 18, 2019 10:40 pm

How do i change the delay using a potentiometer? I'm guessing setting the timer0 to a lower value and having a "delaymain" call delay1 a certain number of times based on the analog value

You could modify your "delay": function to loop a certain number of times, controlled by a variable.
Then you would set the variable to differing values, depending upon an ADC reading of the potentiometer value.

Also, why do this code only light 1 led at the time, theres is probably an obvious reason why i dont need to write BCF PORTC,3 after each to turn off the led

You have just (re)discovered a problem endemic to these old PIC devices. Any PIC16 device with three digits after the "F" is quite old.
When you use a BSF or BCF instruction on a PORTx register, it reads the entire 8 bit port value from the pins, modifies the specified bit, then writes back the entire 8 bit port.
In your case, you are using PORTC. In a PIC16F690, most PORTC pins can be used as analog inputs, and on power up they default to analog mode.
A pin in analog mode will always read as zero, so when you use BCF or BSF on PORTC, all the analog pins read as zero, and will be rewritten as zero, regardless of what you previously write to them. Only the one bit that you actually modify in this instruction will stay set.
If you have another look in the datasheet for the PIC16F690, in the "I/O ports" section, the example for "Initializing PORTC" specifically shows a write to ANSEL to switch the pins to digital mode. Now you know why.

FYI, all the newer PIC16F devices (four or five digits after F) have an extra set of LATx registers which avoid this problem altogether. Yet another reason why you should avoid the old PICs.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 509
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: beginners help with a simple program (on a 16f690 or 12f

Postby QuantumDuck » Sat Oct 19, 2019 7:58 pm

Thanks for the help
This is the code now and it seems to work.
Code: Select all
#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF)
     cblock     0x20
   RESULTHI
     endc

org 0
Start:
   bsf    STATUS,RP0   ;banksel option_reg better?
   movlw   b'00000100'  ;init timer0 med max prescale
   movwf   OPTION_REG
   banksel TRISC
   movlw b'00000000' ; all out
   movwf TRISC
   banksel TRISA
   movlw b'00000001' ;RA0 bit 0 in
   movwf TRISA ;BCF STATUS,RP0 ;back to Register Page 0
   banksel ANSEL
   movlw b'0000' ;AN0/RA0 analog
   movwf ANSEL
   goto mainloop

maindelay
   BANKSEL ADCON1 ;
   MOVLW b'01110000' ;ADC Frc clock
   MOVWF ADCON1 ;
   BANKSEL TRISA ;
   BSF TRISA,0 ;Set RA0 to input
   BANKSEL ANSEL ;
   BSF ANSEL,0 ;Set RA0 to analog
   BANKSEL ADCON0 ;
   MOVLW b'10000001' ;Right justify,
   MOVWF ADCON0 ; Vdd Vref, AN0, On
   ;CALL delay1 ;Acquisiton delay
   BSF ADCON0,GO ;Start conversion
   BTFSC ADCON0,GO ;Is conversion done?
   GOTO $-1 ;No, test again
   BANKSEL ADRESH ;
   MOVF ADRESH,W ;Read upper 2 bits
   MOVWF RESULTHI ;store in GPR space
   call delay1
   DECFSZ RESULTHI,1 ;dec RESULTHI
   btfss RESULTHI,1
   goto $-3 ; loop until RESULTHI = 0
   return


delay1
   btfss   INTCON,T0IF
   goto    $-1
   bcf      INTCON,T0IF
   return

mainloop:
   BCF PORTC,3 ;turn off LED C3
   BSF PORTC,0 ;turn on LED C0
   call maindelay
   BCF PORTC,0
   BSF PORTC,1
   call maindelay
   BCF PORTC,1
   BSF PORTC,2
   call maindelay
   BCF PORTC,2
   BSF PORTC,3
   call maindelay
   GOTO mainloop
end


Will build the project on a breadboard tomorrow and test it.
Feel free to point out any obvious flaws or if there's better ways to make this. A lot of it is copied bits from example files and datasheets, guessing that it's far from optimized code
QuantumDuck
 
Posts: 3
Joined: Wed Oct 16, 2019 5:00 pm

Re: beginners help with a simple program (on a 16f690 or 12f

Postby ric » Sun Oct 20, 2019 6:30 am

All this code only needs ot be run once
Code: Select all
       BANKSEL ADCON1 ;
       MOVLW b'01110000' ;ADC Frc clock
       MOVWF ADCON1 ;
       BANKSEL TRISA ;
       BSF TRISA,0 ;Set RA0 to input
       BANKSEL ANSEL ;
       BSF ANSEL,0 ;Set RA0 to analog
       BANKSEL ADCON0 ;
       MOVLW b'10000001' ;Right justify,
       MOVWF ADCON0 ; Vdd Vref, AN0, On

Move it out of your "maindelay" subroutine to the initialisation code before your first "goto mainloop"

Don't ever got calculated GOTOs unless there is no other way to do it.
Here:
Code: Select all
       BSF ADCON0,GO ;Start conversion
       BTFSC ADCON0,GO ;Is conversion done?
       GOTO $-1 ;No, test again

They are extremely prone to error, and make your code very hard to follow when you come back to it days later, or when someone else tries to understand it.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 509
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 1 guest

cron