PIC18F1220

smooth turning
I was thinking it would look cool for a robot to have smooth turning where it can have the outside wheel/track go faster than the inside one and make a nice rounded turn. this wouldn’t be something i’d implemnet form the start but it would be a feature i’d eventually add to make the robot look smarter. I’m going to have to look into software pwm since I dont think I would be able to control a servo and motors off of the same hardware pwm.

It is interesting how

It is interesting how learning assembly has made my programs in high level languages so much simpler (and most likely more efficient). there are some consepts that just don’t make sense untill you realize how the code is actually being processed.

something wierd is going on.

something wierd is going on. my pwm stopped working all of the sudden. I had it running when i was working on an update in the code and when i was goint to take the chip out to program it the led that was displaying the pwm wasnt lit so i just put my new program on it. It didn’t work so I went back to one of my known working test programs where I have a led flash. that worked but after a few seconds the pic goes into a unstabel state where it seems to have a super fast frequency, I cant figure out the soruce that is causing the clock to go into a super fast state.

what affect does low voltage have on the microcontroller? I havent used these batteries that much but its possible that the voltage could be a little on the low side. i’m using 2 rechargeable batteries (1.2V each) so they would only need to drop .4volts to be at the low end of the recommended voltages for this pic. I wouldnt think that a low voltage would cause the pic to go into this wierd state.

EDIT:

I think i got this figured out. its most likely a low voltage issue. I stuck in the batteries from my mouse and it worked fine. I think the pic kept resetting itself because the voltage is low. the odd thing is I have another pic (16F84A) that works just fine wiht the lower voltage.

SRF05

I’m trying to use the data from the SRF05 to adjust a pwm to show how close something is. the problem seems to be that I’m not getting the echo pulse from the SRF05. I added some led’s that tell me where in the code the pic is and its still waiting for the pulse from the SRF05.

can the SRF05 operate with 2-3V? the datasheet says that one of the holes is for the 5V input but it doesn’t seem to state if it will operate at a lower voltage. Could this low voltage be my problem?

Frequency vs Mark:Space ratio

 

Yes, but this has NOTHING to do with frequency. PWM output is about the amount of time spent high versus the amount of time spent low. This is called the "mark to space ratio." The falative frequencies of the teo motors are completely unimportant. Two motorsd controlled by PWM can go at two different speeds using the same frequency. They use different mark to space ratios.

 

Quite
Yes. I noticed the same thing. My high level programs are now much more modular and I tend not to use the most high functionality!

Typical
I think your V typical should be as close as posible to 5V. The voltage has an affect on the internal clock rate, comparitors, everything. If you’re using only two cells, you are operating pretty close to V minimum.

Easily
It could easily be the problem.

must be my program
I went and got a 4-AA battery holder and 2 more rechargeable batteries and that put me up to 4.92V I still have the same problem. there must be a problem with either my logic or the configuration bits.

Version Control

I do this thing where I stick two digits on the end of the .ASM file every time I make a change. That way I can roll back to teh last version which worked.

Make a program which just switches one bit on and off. See if it works. Make a delay loop. At 4MHz, a loop of 255 NOPs takes around 750us. (NOP=1, jump=2 times 256). Call that 256 times in a loop and you have a 200ms delay in between each toggle of the bit. Enough to be visible.

 

**I do a similar thing. **
I try to start with simple things and get them working and then i put it all together. here is the code i’ve come up with

bsf PORTA,0 ;give the srf05 a pulse to begin
call delayby10us ;call the 10us delay
bcf PORTA,0 ;pull pin back low for end of pulse
clrf range
A1LOW btfss PORTA,1 ;wait for the srf05 to give us a signal
goto A1LOW
A1HIGH incf range
call delayby50us
btfsc PORTA,1
goto A1HIGH

movff range,CCPR1L ;set the pwm duty cycle according to the distance

 

; *****************************************************************
; The next subroutine creates a time delay that is a multiple of
; 10 us. The multiple is passed in PRODL.
; *****************************************************************
delayby10us:
movlw 0x80 ; enable TMR0 with prescaler set to 2
movwf T0CON ; "
loopda: movlw 0xFF ; count up from 2^16 - 40
movwf TMR0H ; and overflow in 200 clock counts
movlw 0xD8 ; "
movwf TMR0L ; "
bcf INTCON,TMR0IF
waita: btfss INTCON,TMR0IF
bra waita
decfsz PRODL,F
bra loopda
return


the all the other delays follow the same format with different values for the prescaler and TMR0 H/L values

Delay

Hmmm… Are timer 0 interrupts definitely disabled?

What happens if you just use your delay in between switching an LED on and off?

I got it to a working state
I got it to a working state now. I’ll check to see if the interupt is truely disabled. currently there is no subroutene for handeling interupts. I just have the pic return to executing the code. I’m still fairly new to interupts and all of this. In the class i’m taking in school on the pic 18f8680, we barely touched on interupts. we have been doing basic things like turning on led’s or generating a pulse for a speaker.

Yes…

…and there may be your problem. If you have no interrupt handler, then every time an interrupt occurs, your program will jump to address 4 (or whatever the interrupt address is) and continue execution from there.

The other thing you can do is step though the program in the IDE. The IDE is absolutely brilliant for debugging. Second to NONE!!

I guess I wasn’t all that

I guess I wasn’t all that clear.

It is my understanding that the code below will retrun the pic to wherever it was before an interupt occured.

org 0x00
goto start
org 0x08
retfie
org 0x18
retfie

I always step through my program in the IDE but I haven’t figure out how to use it to simulate a timer or interrupt.

the SRF05 seems to be working quite well now. I’ve been working on cleaning up the code and adjusting the delays so its more responsive. you can see my blog post for the current code if you like. https://www.robotshop.com/letsmakerobots/node/6076

Looks good

OKay, your chip has two interrupt vectors? At 0x08 and 0x18? If so, then yep, this will work. Just make sure that code goes before any other code to make sure it’s all located beyond 0x19.

Ah. Stimulate is great. Look at the triggers. You can even do an asynchronous serial input.

 

high and low priority interupts

yes, the PIC 18F1220 has 2 interrupt vectors. one is a high priority and the other is a low priority.

quoted form the data sheet
"The PIC18F1220/1320 devices have multiple interrupt
sources and an interrupt priority feature that allows
each interrupt source to be assigned a high priority
level or a low priority level. The high priority interrupt
vector is at 000008h and the low priority interrupt vector
is at 000018h."

 

Internal clock

I’m 99% sure I set the clock to run at 8Mhz but it appears that its running somewhat slower than that. Is there something I could do to test the frequency of the clock? I thought about creating a program that wouldset a port high then low and loop but then i realizd that the goto command also takes time so that would change the frequency if I was trying to measure it. does anyone know of a simple way? my didgital multimeter measures frequency so it’d be a matter of somehow setting a pin to have the same frequncy as the instruction clock.

Reading the datat sheet
looking further into the data sheet found that I can set pin RA6 to FOSC/4 which should give me 2Mhz. I measured it and got 1.986Mhz. This shows that the pic is running correctly and I don’t need to worry about that anymore.

I’m curious, what did you
I’m curious, what did you use to meaure it? Multimeter? Ocilloscope? I’ve got a meter that reads frequncy but think it only goes into the kHz.