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.

What am i doing wrong:'(? Very simple blinking LED

Status
Not open for further replies.

chris414

New Member
Okay so this is my first day getting to use a microcontroller, and I am well and truly stuck. I'm using a PIC18F2550 and am trying to follow this tutorial Tutorial 2 - “Hello World” Program in Assembly Language PIC18F.COM - Tutorials and Sample Code (although the tutorial is written for the PIC18F4550).

So far my code looks like this:
Code:
#include<P18F2550.inc>
	CONFIG WDT=OFF; disable watchdog timer
	CONFIG MCLRE = ON; MCLEAR Pin on
	CONFIG DEBUG = ON; Enable Debug Mode
	CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
	CONFIG FOSC = INTOSCIO_EC;Internal oscillator, port function on RA6
	org 0; start code at 0

Delay1 res 1 ;reserve 1 byte for the variable Delay1
Delay2 res 1 ;reserve 1 byte for the variable Delay2


Start:
	CLRF PORTA ; Initialize PORTA by
				; clearing output
				; data latches
	CLRF LATA ; Alternate method
				; to clear output
				; data latches
	
	CLRF TRISA
	CLRF Delay1
	CLRF Delay2

MainLoop:
	BTG	PORTA,RA5 ;Toggle (20)
Delay:
	DECFSZ    Delay1,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0 
	GOTO	Delay
	DECFSZ	  Delay2,1
	GOTO	Delay

	GOTO	MainLoop
	
	end

When I run the program, the LED just goes on and stays on instead of blinking. By switching the line

Code:
BTG	PORTA,RA5 ;Toggle

with

Code:
BCF	PORTA,RA5 ;Toggle
I am able to turn the light off, and with
Code:
BCF	PORTA,RA5 ;Toggle bit
I am able to turn the light on.

However when I run two BTG instructions right after one another the bit does not toggle the second time the BTG is executed, which makes me think there's something wrong with the toggle??? Any ideas?

Thanks:)
 
The following code works in that it makes the LED flash, but it's clumsy and I want to know why the bit toggling wont work? Anyone?


Code:
#include<P18F2550.inc>
	CONFIG WDT=OFF; disable watchdog timer
	CONFIG MCLRE = ON; MCLEAR Pin on
	CONFIG DEBUG = ON; Enable Debug Mode
	CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
	CONFIG FOSC = INTOSCIO_EC;Internal oscillator, port function on RA6
	org 0; start code at 0

Delay1 res 255 ;reserve 1 byte for the variable Delay1
Delay2 res 255 ;reserve 1 byte for the variable Delay2


Start:
	CLRF PORTA ; Initialize PORTA by
				; clearing output
				; data latches
	CLRF LATA ; Alternate method
				; to clear output
				; data latches


	CLRF TRISA
	CLRF Delay1
	CLRF Delay2






Alpha:
	BSF PORTA,RA5; set on
	DECFSZ    Delay1,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0 
	GOTO	Alpha
	DECFSZ	  Delay2,1
	GOTO	Alpha
Beta:
	BCF PORTA,RA5; clear
	DECFSZ    Delay1,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0 
	GOTO	Beta
	DECFSZ	  Delay2,1
	GOTO	Beta

	GOTO Alpha

	end
 
Last edited:
Hi,

Funny thing - just helped another guy out on the exact same code the other day - but sods law, I cleaned out my directories and the working code has gone.

I didn't like that particular example for a beginner , it uses relocatable code and the delay is only 200ms, so its a fast flash.

I no one else comes back with a working example will post some 2520 code in a little while.
 
Hi,

Better late than never - working example attached for 2550 chip with 4mhz internal oscillator specified.

The code should be built in Absolute mode - you may have to change the setting in Mplab if you have been building in Relocatable.

Shout if it gives you problems.
 

Attachments

  • chris.asm
    2.5 KB · Views: 166
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top