Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Firefly Manual very rough First draft.

Status
Not open for further replies.
I've been so busy I haven't a chance to complete the Firefly manual. I'm posting it in a very rough revision so you can get a glimpse at it and a larger version of the schematic and a parts list.
 

Attachments

  • Firefly Assembly Instructions.pdf
    403.8 KB · Views: 1,192
Last edited:
I've added some code to the Firefly demo program. Blinks the LEDs and reads the A/D port.

In MPASM sometimes it doesn't like a label before an instruction, sometimes it doesn't care...

PS do you prefer mov instructions the old intel way? ie:
mov reg, 0x00
or
mov 0x00, reg

where both instructions mov 0x00 to reg

Code:
;Blinky VR1 requires the use of VR1 (DIP switch VR1 = ON)

        LIST p=16F88
        INCLUDE "p16f88.inc"    
        ERRORLEVEL 0, -302

        __CONFIG _CONFIG1, 0x2F30
        __CONFIG _CONFIG2, 0x3FFC
        
movlf   macro    x,y                     ;MACRO movlf <literal>, <register>
        movlw    x                       ;W = literal
        banksel  y                       ;make sure it's in the right bank
        movwf    y                       ;register = W
        endm                     
        
        cblock   0x20                    ;start of general purpose registers
        Delay_Count                      ;delay loop counter
        endc

        org      0x000                   ;this is where the program starts running      
Init    movlf    0x07, CMCON             ;turn comparators off (make it like a 16F84)
        movlf    b'00000000', ADCON1     ;
        movlf    b'00000010', ANS        ;select RA1/AN1 as analog input
        movlf    b'11001101', ADCON0     ;AD enabled, RC clock, AN1 selected (VR1) & begin conversion


Red1    movlf    b'01000000', PORTA      ;LED1 RED
        movlf    b'10111110', TRISA
        call     Delay

Red2    movlf    b'10000000', PORTA      ;LED2 RED
        movlf    b'00111111', TRISA
        call     Delay

Red3    movlf    b'00000001', PORTA      ;LED3 RED
        movlf    b'01111110', TRISA
        call     Delay

Green3  movlf    b'10000000', PORTA      ;LED3 GREEN
        movlf    b'01111110', TRISA
        call     Delay

Green2  movlf   b'01000000', PORTA       ;LED2 GREEN
        movlf    b'00111111', TRISA
        call     Delay

Green1  movlf    b'00000001', PORTA      ;LED1 GREEN
        movlf    b'10111110', TRISA
        call     Delay

        goto     Red1                    ;repeat forever
        
Delay    
        banksel  ADRESH                  ;
        movf     ADRESH, W               ;W = VR1 value 0x00-0xFF
        banksel  ADCON0                  ;
        bsf      ADCON0,0x2              ;start next conversion
        banksel  Delay_Count             ;
        movwf    Delay_Count             ;Delay_Count = W
        addlw    0x00                    ;used to set Zero flag
        btfsc    STATUS, Z               ;if ADRESH = 0 (Z flag is set)
        return                           ;then return early (no need to delay)        
Loop     
        nop                              ;32KHz Clock (default internal OSC speed)                                             
        decfsz   Delay_Count, f          ;
        goto     Loop                    ;    
        return                           ;return    

        END
 
Last edited:
Here is some example code that reads VR2 (RA3) and positions a servo on con4 (RB3) to correspond to VR2's position. For anyone that already has a Firefly, start a new project in MPLAB, select device 16f88, add a blank .asm file and copy the code below into it, Build and have fun. For anyone without a firefly, this will still work on a breadboard with just a pot on RA3 and a servo on B3, however, it's a lot easier with ICD2 and Firefly. Your power supply should be able to handle the servo current (500mA+).

If there is any interest, I could post other examples such as PC controlled servo, IR remote controlled servo etc.

Mike.

Code:
;###############################################################################
;	Title:			ADC, Servo Demo
;    	Author:			Mike Webb
;	Date:			21 Nov 2006
;################################################################################

		include	"p16f88.inc"

		errorlevel	-302	; stop the annoying " Register in operand not in bank 0." error.

	__config  _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_ON & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
	__config  _CONFIG2, _IESO_OFF & _FCMEN_OFF

; Setup Variables
		cblock	0x20
Count
OnTime
		endc

		org     0h
		nop	;		required for ICD2
		bcf	STATUS,RP1;	bank 0 or 2
		bsf	STATUS,RP0;	select bank 1
		movlw	(0<<NOT_RBPU|0<<INTEDG|0<<T0CS|0<<T0SE|0<<PSA|B'000'<<PS0)
		movwf	OPTION_REG;	setup option reg
		bcf	TRISB,3	;	make b3 output
		movlw	B'111'<<IRCF0;	=b'01110000'
		movwf	OSCCON;		select 8MHz clock
		movlw	b'00001000';	Select bit 3	
		movwf	ANSEL;		ADC on RA3
		bcf	STATUS,RP0;	back to bank 0
Loop		Call	ReadADC;	read A3
		movwf	OnTime;		Store it for later
		movlw	d'250';		250*8 = 2000 cycles = 1mS
		bsf	PORTB,3;	Turn output on
		call	DelayW;		delay the 1mS
		movfw	OnTime;		Get value from ADC
		btfss	STATUS,Z;	if it's zero don't do any delay
		call	DelayW;		delay W*8 = 0 to 255 = 0 to 1.03 mS  -- near enough
		bcf	PORTB,3;	Turn output off - pulse is complete
		comf	OnTime,W;	Complement ADC value
		addlw	1;		com and inc = negate.
		btfsc	STATUS,Z;	Don't do a delay of zero
		call	DelayW;		delay (256-ADC value)*8 = time required to get to 2mS 
		movlw	d'18';		We need to delay another 18mS
		movwf	Count;		so setup a counter
TimeLoop	movlw	d'250';		250*8 = 2000 clock cycles = 1mS
		call	DelayW;		do the delay
		decfsz	Count,F;	repeat it?
		goto	TimeLoop;	yes, go around again
		goto	Loop;		Go back and do it all again

DelayW
; will delay W*8 cycles including the call/return
		addlw	0xff;		add -1 to W			1
		btfsc	STATUS,Z;	=zero?				1/2
		goto	DoneDelay;	yes, better get out of here	2
		nop;			delay a bit			1
		goto	$+1;		delay a bit more		2
		goto	DelayW;		go around again			2
DoneDelay	return	;						2

ReadADC		movlw	b'01'<<ADCS0|b'011'<<CHS0|b'0'<<GO|b'1'<<ADON
		movwf	ADCON0;		select pin A3 and 16 TOSC
		bsf	STATUS,RP0;	bank 1
		movlw	b'0'<<ADFM|b'1'<<ADCS2|b'00'<<VCFG0
		movwf	ADCON1;		Left justify
		bcf	STATUS,RP0;	back to bank 0
		movlw	5;		wait 5*8 = 40 cycles
		call	DelayW;		for ADC capacitor to charge
		bsf	ADCON0,GO;	Start the conversion
WaitADC		btfsc	ADCON0,GO;	Conversion complete?
		goto	WaitADC;	No, so keep waiting
		movfw	ADRESH;		return with result in W
		return

		END
 
I built the firefly, flipped dip switch 1 and 4 on. Chip programmer fine in debugger. I ran the Blinky routine and no LEDs were blinking (mind you I did not test all the LEDs, but they were new). I figured I would put a breakpoint at Red1, and see the debugger running but never stop. So I moved the breakpoint to Init and still it runs and never stops.

I figure all is well and it programs (so VPP, VCC, GND, clock and data) are there? The 16F88 is new from the tube. Any ideas? I have to get started on food for tomorrow, but will pop back in.. Sort of weird though, all went so well, then no LED blink and I realized breakpoints are not working either. I used int-osc RA6 I think. I will check that later too.
 
Used MPLABS, I really like it. Normally .HEX files from MikroBASIC..

First problem was I saved the files (from the web with a cut and paste) into a Unix type editor. Resaved them with DOS edit (CR/LF vs LF) that allowed pommies code to assemble.

But Blinky throws off

Error[113] D:\PIC\16F88\ADCLEDFF.ASM 12 : Symbol not previously defined (ANS)
Error[113] D:\PIC\16F88\ADCLEDFF.ASM 13 : Symbol not previously defined (ANS)

Made ANS now ANSEL and it assembled. Will see if that was the issue.
 
Last edited:
Ahh that sounds like it, I'll post the .asm and .hex files here. I'll post the files on my site too.

One thing that happens with all the Fireflys I've built is a blank 16F88 will turn LED #2 on (Green). Sort of a simple self test. It should do it on all of them, just erase the 16F88 and run it.
 

Attachments

  • Blinky16F88.zip
    1.3 KB · Views: 257
Last edited:
interfacing ADC0804 to PIC16F84

Hello,
I am really new at assembly language programming, and I'm trying to learn how to program a pic to read ADC. can someone send me a sample code/guide me thru how a pic can read the ADC?
 
alexis_ezekiel said:
Hello,
I am really new at assembly language programming, and I'm trying to learn how to program a pic to read ADC. can someone send me a sample code/guide me thru how a pic can read the ADC?

Check my tutorials!.
 
alexis_ezekiel said:
Hello,
I am really new at assembly language programming, and I'm trying to learn how to program a pic to read ADC. can someone send me a sample code/guide me thru how a pic can read the ADC?

The third post in this thread had code to read the ADC.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top