Newbies: Want to use enhanced core chips?

Enhanced mid-range devices. PIC12F1xxx and PIC16F1xxx

Newbies: Want to use enhanced core chips?

Postby Tom Maier » Sun Jun 01, 2014 3:08 pm

If you are new to embedded programming and intimidated by all the complexity of setting up and using these new, powerful chips, check out Great Cow Basic. All the complexity is hidden away, but you have the power to control serial ports, PWM, and many other features.

Here's an example of a Great Cow Basic program that uses the hardware PWM. It uses the enhanced core pic12f1822 and it brightens the LED and then dims an LED in a loop.

Code: Select all

'output a pwm signal on ccp1.
'this is only an 8 bit output

#chip 12F1822, 4
#config osc = int

DIR PORTA.2 out      'this is the PWM output channel 1
         ' the 12F1822 only has one channel (ccp1)
   
    'Main code
    do   
        'Turn up brightness over 2.5 seconds
        For Bright = 1 to 255
            HPWM 1, 40, Bright
            wait 10 ms
        next
        'Turn down brightness over 2.5 seconds
        For Bright = 255 to 1
            HPWM 1, 40, Bright
            wait 10 ms
        next
    loop



Wasn't that easy? Openning a serial port and saying hello is also easy.

Here's what assembly code that the BASIC compiler created for the PWM above...

Code: Select all
;Program compiled by Great Cow BASIC (0.9 19/7/2011)
;Need help? See the GCBASIC forums at http://sourceforge.net/projects/gcbasic/forums,
;check the documentation or email w_cholmondeley at users dot sourceforge dot net.

;********************************************************************************

;Set up the assembler options (Chip type, clock source, other bits and pieces)
 LIST p=12F1822
#include "P12F1822.inc"
    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF

;********************************************************************************

;Set aside memory locations for variables
DELAYTEMP   EQU   .112
DELAYTEMP2   EQU   .113
SYSDIVLOOP   EQU   .116
SYSDIVMULTA   EQU   .119
SYSDIVMULTA_H   EQU   .120
SYSDIVMULTB   EQU   .123
SYSDIVMULTB_H   EQU   .124
SYSDIVMULTX   EQU   .114
SYSDIVMULTX_H   EQU   .115
SysCalcTempA   EQU   .117
SysCalcTempA_H   EQU   .118
SysCalcTempB   EQU   .121
SysCalcTempB_H   EQU   .122
SysCalcTempX   EQU   .112
SysCalcTempX_H   EQU   .113
SysWaitTempMS   EQU   .114
SysWaitTempMS_H   EQU   .115
BRIGHT   EQU   .32
CCPCONCACHE   EQU   .33
PR2_TEMP   EQU   .34
PR2_TEMP_H   EQU   .35
PWMCHANNEL   EQU   .37
PWMDUTY   EQU   .38
PWMFREQ   EQU   .39
SysTemp1   EQU   .40
SysTemp1_H   EQU   .41
SysTemp2   EQU   .42
SysTemp2_H   EQU   .43
T2_PR   EQU   .44

;********************************************************************************

;Vectors
   ORG   0
   goto   BASPROGRAMSTART
   ORG   4
   retfie

;********************************************************************************

;Start of program memory page 0
   ORG   5
BASPROGRAMSTART
;Call initialisation routines
   call   INITSYS
   call   INITPWM

;Start of the main program
   banksel   TRISA
   bcf   TRISA,2
SysDoLoop_S1
   banksel   BRIGHT
   clrf   BRIGHT
SysForLoop1
   incf   BRIGHT,F
   movlw   1
   movwf   PWMCHANNEL
   movlw   .40
   movwf   PWMFREQ
   movf   BRIGHT,W
   movwf   PWMDUTY
   call   HPWM
   movlw   .10
   movwf   SysWaitTempMS
   clrf   SysWaitTempMS_H
   call   Delay_MS
   movlw   .255
   subwf   BRIGHT,W
   btfss   STATUS, C
   goto   SysForLoop1
SysForLoopEnd1
   clrf   BRIGHT
SysForLoop2
   decf   BRIGHT,F
   movlw   1
   movwf   PWMCHANNEL
   movlw   .40
   movwf   PWMFREQ
   movf   BRIGHT,W
   movwf   PWMDUTY
   call   HPWM
   movlw   .10
   movwf   SysWaitTempMS
   clrf   SysWaitTempMS_H
   call   Delay_MS
   movf   BRIGHT,W
   sublw   1
   btfss   STATUS, C
   goto   SysForLoop2
SysForLoopEnd2
   goto   SysDoLoop_S1
SysDoLoop_E1
BASPROGRAMEND
   sleep
   goto   BASPROGRAMEND

;********************************************************************************

Delay_MS
   incf   SysWaitTempMS_H, F
DMS_START
   movlw   .142
   movwf   DELAYTEMP2
DMS_OUTER
   movlw   1
   movwf   DELAYTEMP
DMS_INNER
   decfsz   DELAYTEMP, F
   goto   DMS_INNER
   decfsz   DELAYTEMP2, F
   goto   DMS_OUTER
   decfsz   SysWaitTempMS, F
   goto   DMS_START
   decfsz   SysWaitTempMS_H, F
   goto   DMS_START
   return

;********************************************************************************

HPWM
   movlw   1
   movwf   T2_PR
   movlw   .232
   movwf   SysCalcTempA
   movlw   3
   movwf   SysCalcTempA_H
   movf   PWMFREQ,W
   movwf   SysCalcTempB
   clrf   SysCalcTempB_H
   call   SysDivSub16
   movf   SysCalcTempA_H,W
   movwf   PR2_TEMP_H
   movf   SysCalcTempA,W
   movwf   PR2_TEMP
   movf   PR2_TEMP_H,W
   sublw   0
   btfsc   STATUS, C
   goto   ENDIF3
   movlw   4
   movwf   T2_PR
   bcf   STATUS,C
   rrf   PR2_TEMP_H,F
   rrf   PR2_TEMP,F
   bcf   STATUS,C
   rrf   PR2_TEMP_H,F
   rrf   PR2_TEMP,F
ENDIF3
   movf   PR2_TEMP_H,W
   sublw   0
   btfsc   STATUS, C
   goto   ENDIF4
   movlw   .16
   movwf   T2_PR
   bcf   STATUS,C
   rrf   PR2_TEMP_H,F
   rrf   PR2_TEMP,F
   bcf   STATUS,C
   rrf   PR2_TEMP_H,F
   rrf   PR2_TEMP,F
ENDIF4
   movf   PR2_TEMP,W
   movwf   PR2
   bcf   T2CON,T2CKPS0
   bcf   T2CON,T2CKPS1
   movlw   4
   subwf   T2_PR,W
   btfsc   STATUS, Z
   bsf   T2CON,T2CKPS0
   movlw   .16
   subwf   T2_PR,W
   btfsc   STATUS, Z
   bsf   T2CON,T2CKPS1
   bsf   T2CON,TMR2ON
   movf   PWMDUTY,W
   movwf   SysCalcTempA
   clrf   SysCalcTempA_H
   movf   PR2,W
   movwf   SysCalcTempB
   clrf   SysCalcTempB_H
   call   SysMultSub16
   movf   SysCalcTempX_H,W
   movwf   SysTemp1_H
   movf   SysCalcTempX,W
   movwf   SysTemp1
   movf   SysTemp1_H,W
   movwf   SysCalcTempA_H
   movf   SysTemp1,W
   movwf   SysCalcTempA
   movlw   .255
   movwf   SysCalcTempB
   clrf   SysCalcTempB_H
   call   SysDivSub16
   movf   SysCalcTempA,W
   banksel   CCPR1L
   movwf   CCPR1L
   bsf   CCP1CON,CCP1M3
   bsf   CCP1CON,CCP1M2
   bcf   CCP1CON,CCP1M1
   bcf   CCP1CON,CCP1M0
   banksel   STATUS
   return

;********************************************************************************

INITPWM
   movlw   .26
   movwf   PR2
   bcf   T2CON,T2CKPS0
   bcf   T2CON,T2CKPS1
   movlw   .13
   banksel   CCPR1L
   movwf   CCPR1L
   banksel   CCPCONCACHE
   bsf   CCPCONCACHE,DC1B1
   bcf   CCPCONCACHE,DC1B0
   bsf   CCPCONCACHE,CCP1M3
   bsf   CCPCONCACHE,CCP1M2
   bcf   CCPCONCACHE,CCP1M1
   bcf   CCPCONCACHE,CCP1M0
   bsf   T2CON,TMR2ON
   return

;********************************************************************************

INITSYS
   movlw   .135
   banksel   OSCCON
   andwf   OSCCON,F
   movlw   .104
   iorwf   OSCCON,F
   bcf   ADCON0,ADON
   bcf   ADCON1,ADFM
   banksel   ANSELA
   clrf   ANSELA
   banksel   CM1CON0
   bcf   CM1CON0,C1ON
   banksel   PORTA
   clrf   PORTA
   return

;********************************************************************************

SYSCOMPEQUAL16
   clrf   SYSCALCTEMPX
   movf   SYSCALCTEMPA, W
   subwf   SYSCALCTEMPB, W
   btfss   STATUS, Z
   return
   movf   SYSCALCTEMPA_H, W
   subwf   SYSCALCTEMPB_H, W
   btfss   STATUS, Z
   return
   comf   SYSCALCTEMPX,F
   return

;********************************************************************************

SYSCOMPLESSTHAN16
   clrf   SYSCALCTEMPX
   movf   SYSCALCTEMPA_H,W
   subwf   SYSCALCTEMPB_H,W
   btfss   STATUS,C
   return
   movf   SYSCALCTEMPB_H,W
   subwf   SYSCALCTEMPA_H,W
   btfss   STATUS,C
   goto   SCLT16TRUE
   movf   SYSCALCTEMPB,W
   subwf   SYSCALCTEMPA,W
   btfsc   STATUS,C
   return
SCLT16TRUE
   comf   SYSCALCTEMPX,F
   return

;********************************************************************************

SYSDIVSUB16
   movf   SYSCALCTEMPA_H,W
   movwf   SYSDIVMULTA_H
   movf   SYSCALCTEMPA,W
   movwf   SYSDIVMULTA
   movf   SYSCALCTEMPB_H,W
   movwf   SYSDIVMULTB_H
   movf   SYSCALCTEMPB,W
   movwf   SYSDIVMULTB
   clrf   SYSDIVMULTX
   clrf   SYSDIVMULTX_H
   movf   SYSDIVMULTB_H,W
   movwf   SysCalcTempA_H
   movf   SYSDIVMULTB,W
   movwf   SysCalcTempA
   clrf   SysCalcTempB
   clrf   SysCalcTempB_H
   call   SysCompEqual16
   btfss   SysCalcTempX,0
   goto   ENDIF9
   clrf   SYSCALCTEMPA
   clrf   SYSCALCTEMPA_H
   return
ENDIF9
   movlw   .16
   movwf   SYSDIVLOOP
SYSDIV16START
   bcf   STATUS,C
   rlf   SYSDIVMULTA,F
   rlf   SYSDIVMULTA_H,F
   rlf   SYSDIVMULTX,F
   rlf   SYSDIVMULTX_H,F
   movf   SYSDIVMULTB,W
   subwf   SYSDIVMULTX,F
   movf   SYSDIVMULTB_H,W
   subwfb   SYSDIVMULTX_H,F
   bsf   SYSDIVMULTA,0
   btfsc   STATUS,C
   goto   ENDIF10
   bcf   SYSDIVMULTA,0
   movf   SYSDIVMULTB,W
   addwf   SYSDIVMULTX,F
   movf   SYSDIVMULTB_H,W
   addwfc   SYSDIVMULTX_H,F
ENDIF10
   decfsz   SYSDIVLOOP, F
   goto   SYSDIV16START
   movf   SYSDIVMULTA_H,W
   movwf   SYSCALCTEMPA_H
   movf   SYSDIVMULTA,W
   movwf   SYSCALCTEMPA
   movf   SYSDIVMULTX_H,W
   movwf   SYSCALCTEMPX_H
   movf   SYSDIVMULTX,W
   movwf   SYSCALCTEMPX
   return

;********************************************************************************

SYSMULTSUB16
   movf   SYSCALCTEMPA_H,W
   movwf   SYSDIVMULTA_H
   movf   SYSCALCTEMPA,W
   movwf   SYSDIVMULTA
   movf   SYSCALCTEMPB_H,W
   movwf   SYSDIVMULTB_H
   movf   SYSCALCTEMPB,W
   movwf   SYSDIVMULTB
   clrf   SYSDIVMULTX
   clrf   SYSDIVMULTX_H
MUL16LOOP
   btfss   SYSDIVMULTB,0
   goto   ENDIF7
   movf   SYSDIVMULTA,W
   addwf   SYSDIVMULTX,F
   movf   SYSDIVMULTA_H,W
   addwfc   SYSDIVMULTX_H,F
ENDIF7
   bcf   STATUS,C
   rrf   SYSDIVMULTB_H,F
   rrf   SYSDIVMULTB,F
   bcf   STATUS,C
   rlf   SYSDIVMULTA,F
   rlf   SYSDIVMULTA_H,F
   movf   SYSDIVMULTB_H,W
   movwf   SysCalcTempB_H
   movf   SYSDIVMULTB,W
   movwf   SysCalcTempB
   clrf   SysCalcTempA
   clrf   SysCalcTempA_H
   call   SysCompLessThan16
   btfsc   SysCalcTempX,0
   goto   MUL16LOOP
   movf   SYSDIVMULTX_H,W
   movwf   SYSCALCTEMPX_H
   movf   SYSDIVMULTX,W
   movwf   SYSCALCTEMPX
   return

;********************************************************************************


 END



You run that assembly program through the mplabx assembler and use your programmer to blow the code into the chip. Could life get any easier?

Also, the BASIC code is very portable. A couple of small changes and you now have code to do PWM on a pic18.

Great Cow BASIC is a totally free and open source program (for windows only, unfortunately). If you are bogged down in trying to understand the pic hardware or struggling with learning assembly language or C, then try this out.

http://gcbasic.sourceforge.net/

Great Cow also has a Great support forum.
http://sourceforge.net/p/gcbasic/discussion/
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: Newbies: Want to use enhanced core chips?

Postby linker3000 » Tue Jun 03, 2014 6:15 am

Hi Tom,

That's wicked! I am trying to find the time to 'get into' PIC programming as a hobby and I think this would be a great tool to create some simple stuff and then study the assembly code it creates.

L3K
linker3000
Verified identity
 
Posts: 14
Joined: Fri May 30, 2014 3:59 am

Re: Newbies: Want to use enhanced core chips?

Postby Tom Maier » Tue Jun 03, 2014 1:15 pm

This great cow compiler would certainly be the fast track to getting some thing up and runing right away. Notice that the basic profram contains almost no reference to all the complicated set up procedure for the chip. Also, since they have included most of the newest feneration of chips, you are not limited to the old 16f84 clunker stuff.

I was fooling around with learning the 12f1822 and was having trouble setting up some peripherial, and then I tried a quick program in great cow, and POW, I was up and running. I eventually worked out my problems, but I was just amazed that it worked so well.

Learning the assembly language from the output asm of the great cow compiler may not be so good an idea. The code contains a lot of "house keeping" code that the compiler needs to create its structure. You would end up learning more about how a basic compiler works more than how to do assembly coding. To learn pure assembly coding you could go to gooligum's site and check out their free tutorials.

But... I want newbies to know that they don't have to squirrel themselves away for several months to learn programming before they can work with embedded. If they have a basic understanding of programming, they can use this BASIC.

This BASIC is much more limited than assembly programming or XC8. Notice there are no floating point variables and arays can only be bytes. There are ways around most limitations and the people on their forum or on this one might help. Notice that there ARE some sophisticated features like simple interrupt handling, in-line assembly langauge, and fairly decent math functions. What really makes it shine is the ablility to invoke a peripherail with almost no knowledge of the tedious details involved.

People who do embedded for a living need to know all these details to get the maximum use out of the chips, but that really holds the hobbiests and "make" people back, so this is a solution to that.

It is similar to the arduino pseudo-C. It hides the complexity to allow quick development.
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: Newbies: Want to use enhanced core chips?

Postby linker3000 » Wed Jun 04, 2014 2:52 am

Thanks Tom,

I'm an old x86 asm coder from the 1980-90s and although the X86 and PIC architectures are radically different, I'm pretty sound on the algorithm side so, as you mentioned, it will be a boost for device settings and perhaps some otherwise weird gotchas. I agree that it would be counterproductive to learn assembler from scratch this way.
linker3000
Verified identity
 
Posts: 14
Joined: Fri May 30, 2014 3:59 am

Re: Newbies: Want to use enhanced core chips?

Postby Tom Maier » Wed Jun 04, 2014 1:05 pm

Oh... I get it.

I used to do embedded x86 back in the day also. Assembly and C and PLM and ADA.

Yes, the code does reviel how to set up the peripherials. That was what I did when I was having trouble with the pic12f1822. I kept going round-and-round with that chip and then tried the compiler, and found something I had forgotten to do.
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: Newbies: Want to use enhanced core chips?

Postby linker3000 » Thu Jun 05, 2014 3:51 am

..aah...I did a bit of PLM/86 on the big blue Intel development machines - with 12" floppy disks!

That was a really nice language - I loved the way it would highlight syntax errors and suggest what you probably meant to do.

...all good fun - for visual systems on flight simulators! Great fun was had during work breaks!
linker3000
Verified identity
 
Posts: 14
Joined: Fri May 30, 2014 3:59 am

Re: Newbies: Want to use enhanced core chips?

Postby Tom Maier » Thu Jun 05, 2014 2:56 pm

The Big Blue Box! Wow, that brings back memories. We used that for developing embedded x86 code.

We had one floppy disk that somebody had tightened up the ejection spring on it. When you hit the release lever the drive would eject the floppy so violently that it would lauanch that 12" frizbee across the room. Always a good laugh for new programmers.

I think those were 128 KBytes, or 96 KBytes? We used to carry a sachel of floppies around for development. One for OS boot disk, one for assembler, one for compiler, and about six for data, and then some spares.

We had two floppies and no hard drive, so when the blue box would compile it would flip back and forth between the two floppies and make lots of lights and grinding sounds. It often would take about an hour to do a compile. People would wander past the door and look it with wide eyes as the impressive looking box made lots of noise and a light show. Yes, computer have really gone done hill since those days.
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: Newbies: Want to use enhanced core chips?

Postby Joseph Watson » Fri Jun 06, 2014 6:19 am

I believe the INTEL Blue Box 8086 Development Systems used 8 inch floppies. I still have some somewhere in one of my barns.
NCR once refused to hire me because I was too short. I'm still waiting on my growth spurt.
User avatar
Joseph Watson
 
Posts: 49
Joined: Sat May 31, 2014 8:06 pm
Location: Ohio, USA
PIC experience: Experienced Hobbyist

Re: Newbies: Want to use enhanced core chips?

Postby Tom Maier » Fri Jun 06, 2014 12:45 pm

I had to go look that up, it was so many years ago...

220px-Floppy_disk_2009_G1.jpg
220px-Floppy_disk_2009_G1.jpg (7.18 KiB) Viewed 1933 times


Yep, it was 8 inch.

Then the company moved over to this box called "IBM PC". It also had two floppies and no hard drive.

I went out and bought a clone with 640K RAM, 5 MB hard drive, and a CGA color screen with 16 colors. My coworkers laughed when I brought it in. How would a person EVER fill up a 5 MB hard drive? Obviously a case of more money than brains! And 16 colors? Even ANY colors seemed unnecessary.

Ahh, them were the days...

EDIT: Oh, wait... that was a 20 MB hard drive. That was considered a "server size drive" back in those days. Still, considered ridiculously large in those days since most people were just working off floppies.
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

Re: Newbies: Want to use enhanced core chips?

Postby Joseph Watson » Fri Jun 06, 2014 4:20 pm

My first true development system arrived in 1968. It had 4K of 12-bit words of magnetic core memory. It did have a hard drive with a storage capacity of 32K of 12-bit words making it the equivalent of 48 Kilobytes. In that meager space, we kept a text editor, an assembler, a debugger, and the source code of the assembly language program being developed. If the source code exceeded the storage limits (How could that ever happen?), we could also prepare the source file on fan-folded 1-inch wide punched paper tape. The high speed tape punch operated at 50 characters per second and the high speed optical paper tape reader could read the tape at 300 characters per second.

The hard drive did not have a moving head design. It actually had a separate fixed head for each track on the disk.

When working on a program for the 4K word machine, assembling the program and printing the listing from the assembler would take about 4 hours. The printer was a teleprinter with a speed of 10 characters per second. On a machine that takes so long to produce the listing, you "made do" with a previous listing with lots of pencil marks all over it as long as you could.

Anyone who could develop programs for this machine could also readily hand-assemble patches for it. We did it so much that we knew most of the numeric machine language instruction codes by heart. Three of us shared the use of this system. Not knowing what kind of wonders were coming in the future, we thought we had it made back then.
NCR once refused to hire me because I was too short. I'm still waiting on my growth spurt.
User avatar
Joseph Watson
 
Posts: 49
Joined: Sat May 31, 2014 8:06 pm
Location: Ohio, USA
PIC experience: Experienced Hobbyist

Next

Return to 14-Bit Core (enhanced)

Who is online

Users browsing this forum: No registered users and 1 guest