Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 3rd February 2008, 11:39 PM   (permalink)
Default

actually I just took a look at my other 16f690 program code. This is for the counter that I made, which works flawlessly. But I had declared some critical values starting in the 0xC1 range! I am assuming that it only worked because the variables managed to fall somewhere in the GPR range.

I will be reading up on how to use the simulator tonight. I just tested the PIC in the other project, and it works fine. ARG!!!!!!! I am going to have to go line by line and check to see if I accidentally deleted something. MPLAB was behaving oddly the other night...When I clicked on a line it would paste code from somewhere and kept putting breaks in spots....

Last edited by Ambient; 3rd February 2008 at 11:42 PM.
Ambient is offline   Reply With Quote
Old 4th February 2008, 02:07 AM   (permalink)
Default

Quote:
Originally Posted by Ambient
here is the latest code. I removed the IOC. I wanted to learn how to use it, but now I just want to get this thing working.

Here is what the pins are doing: PORTA,B are tied low through 4.7k's, but are giving me 5V at the pins. PORTC pins (some of them) go to 5V when I write all 0's. When I write all 1's the pins bounce all over a 100mv to 300mv range, until I reconnect them to the LED, then they sit at 4V. What the heck is going on?!

oh, and for some reason the picture linker is not working for the post...it didnt show. So I also gave the link.

Code:
List    p=16f690
    include    P16F690.INC
 
    __CONFIG    _CP_OFF & _CPD_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF; & _INTRC_OSC_NOCLKOUT
    errorlevel  -302
 
;-----------------------VARIABLE REGISTER DEFINITIONS---------------------------
w_temp        EQU    0x40        ; variable used for context saving
status_temp    EQU    0x41        ; variable used for context saving
pclath_temp    EQU    0x22        ; variable used for context saving
Digit1        EQU    0x43
Digit2        EQU 0x44
temp        EQU    0x45
 
;================================CODE==================================
    org        0x0000
      goto    Initialize
;***************************Interrupt_Routine**************************
    org        0x0004            ; interrupt vector location
    movf    STATUS,W        ; move status register into W register
    movwf    status_temp        ; save of contents of STATUS register
    movf    PCLATH,W        ; move pclath register into W register
    movwf    pclath_temp        ; save of contents of PCLATH register
;
Hi,
Unless I'm blind , but still, I don't see the w register being saved in the ISR, but only restored. You see it working because it doesn't do anything in the main routine, just idle looping and w is not used. When the time you've got your main to do something, you'll find the occurance error.
__________________
Superman returns..
bananasiong is offline   Reply With Quote
Old 4th February 2008, 02:23 AM   (permalink)
Default

You need to enable global interrupts (INTCON,7) for any interrupts to work. Also, are you using (corrupting) temp in your table call? And, as already mentioned you aren't saving W in your ISR.

Mike.
Edited as I didn't look at all the code.

Last edited by Pommie; 4th February 2008 at 02:30 AM.
Pommie is offline   Reply With Quote
Old 4th February 2008, 07:17 AM   (permalink)
Default

hmmm i guess i wasn't. Thats the last time I copy code from another project. I did not need to save W in my last project, and I recycled some of its code.

GIE was disabled while I tried to figure out why the pins were screwy. I just had the program send a digit manually in the main function.

temp is not used anywhere else except for when separating each nibble. I should have just named it "data" or whatever. The program is doing things in a unorganized way because I started it using IOC. When IOC generated an interrupt, it would run "ReadPort" subroutine, but now it is just set up to read when it switches displays.
Ambient is offline   Reply With Quote
Old 4th February 2008, 08:58 AM   (permalink)
Default

fixed another issue. It turns out that the last bank switch, which was labeled Bank 0, was actually switching to Bank 1. So writing to PORTC was not working. But it was somehow working with that error before today lol. I should not have any more problems, and I will post the end results when they are workin.

Right now the code seems to work better. It displays the test digit sent in main and then half a second later it displays what is on the input port. But it gets stuck there. I will keep working on it.

Thanks guys.

Code:
main
	bsf		INTCON,GIE
	bsf		PORTB,h'04'
	bcf		PORTB,h'05'
	movlw	b'11110100'		;test
	movwf	PORTC
	nop
	nop
	goto	$-1				;idle loop

ReadPort
	movf	PORTB,W
	andlw	b'11000000'
	iorwf	PORTA,W
	movwf	temp			;temp contains full input byte
	andlw	h'0F'			;Least Significant Nibble
	call	Table
	movwf	Digit2
	swapf	temp,W
	andlw	h'0F'			;Most Significant Nibble
	call	Table
	movwf	Digit1

Display1
	call	ReadPort
	bsf		PORTB,h'04'		;disable display 2
	movf	Digit1,W
	movwf	PORTC
	bcf		PORTB,h'05'		;enable display 1
	goto	STATUS_RESTORE	

Display2
	bsf		PORTB,h'05'		;disable display 1
	movf	Digit2,W
	movwf	PORTC
	bcf		PORTB,h'04'		;enable display 2
	goto	STATUS_RESTORE
	
Table
	andlw	b'00001111'
	addwf	PCL,F
	;PORTC    76543210
	;Segment  abcdefgh, active low
	retlw	b'00000010'	;0
	retlw	b'10011110'	;1
	retlw	b'00100100'	;2
	retlw	b'00001100'	;3
	retlw	b'10011000'	;4
	retlw	b'01001000'	;5
	retlw	b'01000000'	;6
	retlw	b'00011110'	;7
	retlw	b'00000000'	;8
	retlw	b'00011000'	;9
	retlw	b'00010000'	;A
	retlw	b'11000000'	;B
	retlw	b'01100010'	;C
	retlw	b'10000100'	;D
	retlw	b'01100000'	;E
	retlw	b'01110000'	;F
	END

Last edited by Ambient; 4th February 2008 at 09:06 AM.
Ambient is offline   Reply With Quote
Old 4th February 2008, 09:17 AM   (permalink)
Default

Your ReadPort routine seems to have lost it's return instruction.

Mike.
Pommie is offline   Reply With Quote
Old 4th February 2008, 08:26 PM   (permalink)
Default

hmmm..that might be important lol. TY now it works perfectly, just gotta adjust the PS for TMR2 and maybe reorganize the code. It is not very efficient right now.

Last edited by Ambient; 4th February 2008 at 08:32 PM.
Ambient is offline   Reply With Quote
Old 5th February 2008, 12:57 AM   (permalink)
Default

Changing banks by doing this:
Code:
	movlw	b'00111000'
	movwf	STATUS			;BANK 1
	movlw	b'01000001'
	movwf	OSCCON			;sets oscillator to 1MHz
is not the best way. One, it's not as clear what you're doing as it could be. Two, wiping out the STATUS register every time you change banks could conceivably cause some serious bugs under the right circumstances. I'd think ORing or BSF/BCFing those bits in there would be safer.

Why not use the banksel directive? Like this:
Code:
	banksel	OSCCON		;bank 1
	movlw	b'01000001'
	movwf	OSCCON		;sets oscillator to 1MHz
Let the assembler do the work for ya. Makes the code nice and readable. And it isn't as prone to operator error as diddling the bits yourself.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 5th February 2008 at 01:15 AM.
futz is offline   Reply With Quote
Old 5th February 2008, 05:33 PM   (permalink)
Default

True. And the code would be a little more portable for different 16f's too. I already have it posted in the projects forum, I will mod the code there.
Ambient is offline   Reply With Quote
Old 5th February 2008, 11:38 PM   (permalink)
Default

Here's another thing you're doing that could (should?) be done differently. Your code has this
Code:
w_temp		EQU	0x40	; variable used for context saving
status_temp	EQU	0x41	; variable used for context saving
pclath_temp	EQU	0x22	; variable used for context saving
Digit1		EQU	0x43
Digit2		EQU	0x44
temp		EQU	0x45
Curious why you're starting your variables at $40 instead of the start of RAM, $20. Datasheet page 31. One is at $22 but the rest are in the $40's.

Why not use a cblock instead? Easier to type. Easier to read.
Code:
	cblock	0x20
	w_temp,status_temp,pclath_temp,Digit1,Digit2,temp
	endc
If you have many variables you can do multiple lines like this (Note, no comma at end of lines):
Code:
	cblock	0x20
	w_temp,status_temp,pclath_temp
	Digit1,Digit2,temp
	endc
Or if you have only a few and want them one per line you can do this:
Code:
	cblock	0x20
	w_temp
	status_temp
	pclath_temp
	endc
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 5th February 2008 at 11:45 PM.
futz is offline   Reply With Quote
Old 5th February 2008, 11:52 PM   (permalink)
Default

When using cblock you can also set the variable size,
Code:
	cblock 0x20
Accumulator:2
CharBuffer:8
TimeBuffer:0
Seconds
Minutes
Hours
;etc
	endc
In the above TimeBuffer and Seconds would both be 0x2a.

Mike.
Pommie is offline   Reply With Quote
Old 5th February 2008, 11:56 PM   (permalink)
Default

Quote:
Originally Posted by Pommie
When using cblock you can also set the variable size,
Excellent! I did not know that.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================
futz is offline   Reply With Quote
Old 7th February 2008, 04:11 AM   (permalink)
Default

Thanks guys. When I was starting out programming I avoided using cblock and banksel and others like it. I felt that I should do things manually so that I understood everything that was happening. But now I guess I should start using them.
Ambient is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Quik PIC Programming kit Krumlink General Electronics Chat 5 27th January 2008 11:27 PM
Capturing and reproducing audio with a PIC Fred.Amoson Micro Controllers 14 14th December 2007 08:21 PM
Problems switchin relay with PIC Andy1845c General Electronics Chat 5 17th November 2007 06:13 PM
High ADC sampling rate PIC, 18F needed? bananasiong Micro Controllers 24 28th October 2007 12:13 PM
Four PIC with One LCD.. meera83 Micro Controllers 13 20th September 2007 06:40 AM



All times are GMT. The time now is 11:27 PM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.