Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Please help me convert my idea into C code

Status
Not open for further replies.

Neyolight

Full Member level 5
Full Member level 5
Joined
Aug 29, 2011
Messages
306
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
World
Activity points
3,624
HI ALL

Well I am using PIC 18F4620 and coding in MC18. I want to do something and I have a clear idea about it but unsure how to code that in C. Could you guys please have a look at what I want to do and convert that to C code please.

So, I am using interrupts. I have a test condition in the main loop. Before entering the interrupt the code must check weather the test condition has reached. If yes, the code go to interrupt if interrupt occurs. If not, it stays there till the test condition has been met.

Now once the code go to an interrupt routine, it does what its suppose to and RETURN back to the TEST in the main loop for another trial. I have an image attached of what I want to do.
 

Hello!

You should download some simple examples and run them with debugger to see what happens.
By the way, what do you mean by "when RC0 = 0, when RC0 = 1". This cannot be true at the
same time, so you have to make a choice.
And the definition of what you plan to do is quite confusing.
-> If yes, the code go to interrupt if interrupt occurs
You should write your block diagram with standard items. Tests should be ♢ shaped and you
should have only one test in a ♢. And the branches should go to clear statements.

"Code goes to interrupt" is not a clear statement, it can mean:
1. Code goes to interrupt service routine (i.e. because interrupt has happened)
2. Code goes to interrupt wait state (i.e. because interrupt has not happened).

Dora.
 

Hey thanks, well I have some code right now but it just wont go to the ISR after the test condition is met.

So basically I have a TTL input at RC0. I want to enable ISR routine at the FIRST rising edge and hence testing RC0 = 1 and RC0 = 0.

So what I want to do is, when the test condition is achieved the interrupt is enabled and the code must WAIT for interrupt to occur but it just loops through the entire code over and over again.

---------- Post added at 16:14 ---------- Previous post was at 16:14 ----------

I will make a clear flow chart and paste soon.

---------- Post added at 16:38 ---------- Previous post was at 16:14 ----------

After an ISR routine finishes, the program counter returns to main or the endless while loop in main ?

I have an IF loop in main that it skips after returning from ISR.
 

Please read the interrupt chapter of the microcontroller datasheet,
maybe you can get a better idea...

An interrupt es an 'event' that happens inside the microcontroller, this 'event' is 'triggered' by internal or external means... (say a edge detected on an special input pin, or a timer overflow, or some communication event)

this 'events' can be enable or disable (all except a reset)
and when they are enabled properly, they will issue an 'interrupt' in the microcontroller...

so it will interrupt normal code execution, an 'call' an interrupt subroutine at an specific location (in case of a PIC18 there are two possibilities, normal/high priority at 0x0008 and low priority at 0x0018)
[don't forget it's an special case of an automatic 'call' so it will preserve some info to 'return' (like the program counter to return to)]

of course inside your 'main program' you can enable or disable the interrupts at will, but hardly you can test a condition for an interrupt BEFORE executing the interrupt itself! the common workaround is to go inside the interrupt and the first thing is check the condition.. if false exit inmediatly the interrupt without further execution... if true do the tasks you planned to.


Also, don't forget the external interrupts (like an edge detect on an input pin) mostly CAN'T BE ENABLED FOR ANY PIN (at least in most PIC18 ) sadly i don't have the specific datasheet for your pic, and most pic18 have the external interrupt on pins RB0,RB1 and/or RB2, so please re-check that you can have an edge detect interrupt in your RC0 pin...




now... maybe you can describe further your complete system (not necesarly a flow-chart, maybe a state-machine or graphset) including: the normal execution (what is your program doing), the interrupt procedure (what will interrupt and what it wil do).

Also remember, if you wait, iddle for an interrupt... mmm it's not an proper use for an interrupt... it's better to make a simple call to a procedure...
 
Interrup Service Routine(){
-Do what you want to do when interrupt occurs.
}

main{
Enable Interrupts;
while(1);
}
 
Hey thanks for that, I will add the layout of the code in an hour or so !
 

Hi,

You have two options for interrupts on PortB, RB4-7 allows it to sense any change to the current state, RB0-3 allows you to sense a change of the signal, you can select if it detects on leading edge low to high or falling edge high to low.

Don't forget to configure PortB for digital as it defaults to Analogue on RB0-3. You can use Configuration bit PBADEN to change that to Digital by default.

Good to see somone else useing a flow chart - I still find them really helpful in properly planning your code - wish a few more folk would do the same !
 

I want my free running timer0 to start incrementing at the first RISING edge at RC0. In clear words, as timer1 increment from FFFD to FFFE, timer 0 should start incrementing from 0 .:roll: Is it even possible to do so?

---------- Post added at 10:40 ---------- Previous post was at 09:22 ----------

And all of a sudden I am unable to write to TMR1H ( Timer 1 high byte) register. Unsure why? :S
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top