I am learning to use the MCF51JF128 board and am attempting to write a simple program to control the onboard green and orange LEDs on the MCF51JF128 board. (on my own - not a school project)
I can not for the life of me understand what I am missing that is keeping them from functioning. I have studied the quick start demo that freescale provides, but it seems needlessly complicated for my purposes. (tons of linked files throwing errors when trying to prune it down).
I currently have the code attached below (Don't worry - it's short) . What am I missing that would make the LEDs not respond? They respond correctly in the quickstart demo program, but I don't know what I am missing here.
/*
* LEDs.h
*
* Created on: Apr 4, 2011
* Author: B23243
*/
#ifndef LEDS_H_
#define LEDS_H_
#define LED1_ON PTA_D &= 0xFE
#define LED2_ON PTC_D &= 0xDF
#define LED1_OFF PTA_D |= 0x01
#define LED2_OFF PTC_D |= 0x20
#define LED1_TOG if(PTA_D & 0x01) PTA_D &= 0xFE; else PTA_D |= 0x01
#define LED2_TOG if(PTC_D & 0x20) PTC_D &= 0xDF; else PTC_D |= 0x20
#endif /* LEDS_H_ */
**************************************************************************************************************************
#include "common.h" /* include peripheral declarations */
#include <LEDs.h> /* code to toggle pins connected to LEDs */
/* function prototypes */
void disable_watchdog(void);
void LEDs_Init(void);
/* variables*/
unsigned char state;
/* functions */
void disable_watchdog(void)
{
SIM_SCGC4 = SIM_SCGC4_WDOG_MASK;
SIM_COPC = SIM_COPC_COPT(0);
}
void LEDs_Init(void)
{
MXC_PTAPF4 = ((MXC_PTAPF4 & 0xF0) | 0x01);//Select PTA0 GPIO
MXC_PTCPF2 = ((MXC_PTCPF2 & 0x0F) | 0x10);//Select PTC5 GPIO
PTA_DD = 0x01; //Configure PTA0 as output
PTC_DD = 0x20; //configure PTC5 as output
LED1_ON;
LED2_ON;
}
/* main */
void main(void) {
EnableInterrupts;
/* include your code here */
LEDs_Init();
disable_watchdog();
for(; {
} /* loop forever */
/* please make sure that you never leave main */
}
I know this post is well over a year old and OP has surely moved on but I'd like to share the solution :smileysilly:
You have to enable the clock for port C or it won't do... anything. Set bit 2 of the SIM_SCGC6 register. SIM_SCGC6 is documented in the reference manual for the MCF51JF128 in section 13.2.18.
Hope this helps someone in the future.
Edit: I was only using the LED on port C. Obviously you should enable the clock for port A if you want the other one too :smileyblush:
Jeff Roberts wrote:
I know this post is well over a year old and OP has surely moved on but I'd like to share the solution
You have to enable the clock for port C or it won't do... anything. Set bit 2 of the SIM_SCGC6 register. SIM_SCGC6 is documented in the reference manual for the MCF51JF128 in section 13.2.18.
Hope this helps someone in the future.
Edit: I was only using the LED on port C. Obviously you should enable the clock for port A if you want the other one too
Hello from the future Another "bare metalist" here, ran into the same problem on MCF51JF128 and was about to give up until I found this post. Signed up here today just to thank you guys! Hopefully the next revision of the Reference Manual will have some mention of clock gating in the EGPIO section.
Well spotted. This family of chips has clock gating to every peripheral, and all that can default to OFF on Reset and Power On.
Which is why it is worth sitting down and reading the manual from cover to cover before asking questions, or before starting coding. This feature of the chip is clearly detailed in multiple places, but you have to read everything to find those references. If the ports aren't working (the OP's problem ) you wouldn't think of reading the "Clock Gating" chapter to find the following:
5.2.5 Clock gating
The clock to each module can be individually gated on and off using the SIM's SCGCx
registers. These bits are cleared after any reset, which disables the clock to the
corresponding module to conserve power. Prior to initializing a module, set the
corresponding bit in SCGCx register to enable the clock. Before turning off the clock,
disable the module or place the module in its default configuration.
Turning off the clock for an enabled, working module can produce unexpected behavior.
There is no mention of this clock gating in the EGPIO chapter at all. it is a "Standard Module" that is used in multiple chips, and the "Clock Gating" is a feature of the chip provided in another module..
The OP said:
> I have studied the quick start demo that freescale provides, but
> it seems needlessly complicated for my purposes.
I would say NECESSARILY complicated. These are very complicated chips. If you want something you can get working easier, then start with a 6800, 2650, 68HC11 or Z80 from 30 years ago. :-)
But you really don't want to code these things from scratch. It may be satisfying to write all the code for a product "on the bare metal", but it sure isn't productive.
Tom
Hmmm.
When I run this with the rest of the files from the downloaded MCF51JF128 demo it works as expected, but when I am running with just these two files (plus expectations.c and the other auto-generated files) it doesn't work. What other factors besides the code I provided are necessary to make the LEDs work?
Thanks for all the help
Have you singled stepped thru the code in the debugger as suggested?
What I have done in similar cases is to look at each IO register before and after writing to it so see if the expected things are happening.
Another idea is to create a processor expert project and just define two "bits' assigned to the ones you are using. This code should be simpler.
Onviously all that other code is doing something you need to do. You've cut it down too much.
The trick now is to try and find out what it is.
Here are two classic ideas:
1 - Modify the working code and your non-working code to dump all the peripheral registers somewhere. Print them in hex to a serial port or dump them via a debugger - whatever is easiest. Then COMPARE the dumps to track down what is different. That should lead you to what you're missing.
2 - Starting with the working code, start commenting out slabs of the initialisation code until it stops working. You can use a "binary chop" (look that up) to make this pretty quick. The last small bit you commented out is the bit you need.
Of course the above assumes that your cut-down code is actually running and is not locked up somehow because you forgot to initialise the stack pointer or something. Given it can't even turn a LED on, how do you know it is running at all?
Tom
I can't spot any obvious problems.
The problem may be "which peripheral has got those pins"?
I don't have the manual for your exact chip here, but looking through the MCF51JE256 manual has me confused. "Pin Assignments" gives GPIO and Alternate functions for the port pins (PTA0/FB_D2/SS1 etc), but I can't find a single register that sets the assignments for these pins like I'm used to in the MCF53 series.
I've found the following sentence: "MCF51JE256/128 uses the HCS08 style of pin muxing to be Flexis-compatible. The pin defaults as GPIO and the alternate functions follow a priority where ALT3 has the highest priority."
I think that means if the peripheral is enabled then it "wins". So if you have SS1 or the Flexbus enabled then PTA0 won't be GPIO.
Do you have a debugger? Can you write to the registers from the debugger while looking to see if the LEDs come on? That's usually easier than writing code and hoping it is doing what you expect. Can you get a LED, meter or CRO to a pin that doesn't have an alternate function (possibly PTA4-7) and see if that works first?
Tom
I do agree with tom that there is not an obvious problems.
Tom, if you look at pg 211 of the JF RM you will see that MXC_PTAPF4 is the configuration register, and that writing 1 to the lower nibble sets the pin function to "PTA0". Not that this is much help to the OP.
If you look in the "data sheet" 8.1 "Pin assignments" it seems setting the MCX register to 1 in the repective nibble selects the GPIO function....
There is something else going on that is not seen in the code...