Hi everyone. I’m trying to program a very simple application that adds one unity to the outputs of one port (P4), but it has to be as faster as possible. My problem is the speed. The maximum time rating with external clocking is 8 MHz, but such a simple program only arrives to some kHz. I leave you the program. If you have any answer to my question, please, tell to me. The program:
#include <msp430x16x.h>
unsigned int i = 0; // Variable global
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
BCSCTL1 = XTS + DIVA_0; // XT2 ON, modo alta frecuencia, sin división.
BCSCTL2 = SELM1 + DIVM_0 + SELS + DIVS_0; // Fuente del MCLK: XT2.
P4DIR |= 0xFF;
P4OUT = 0x00;
//P1SEL &= ~BIT2;
P1IE = BIT2;
P1IES = 0xFF; // Flanco de subida de reloj.
//ADC12CTL0 = REF2_5V + REFON; // Internal 2.5V ref on
First, the simple one. If P1.2 generates an interrupt, the variable i will be changed to 1. Thus the do {P4OUT++;} while (i==0); loop will terminate. My question is, what will happen after that? Your main() ends there and thus will “return”. But “return” to what?
The second question is about the 8 MHz clock. Is the crystal connected to XIN & XOUT or XT2IN & XT2OUT? Is it working (oscillating)? One way to check this is include the code at the beginning:
P5OUT = 0x70;
P5SEL = 0x70;
and use an oscilloscope to look at P5.6, P5.5. and P5.4. You should see ACLK, SMCLK, and MCLK respectively.
ok, first of all, i have included the interruption cause i need that at a determined time, the value displayed by the p4 output is constant.
the external clock is conected to xt2out and xt2in, it works well. I have verified it with an oscilloscope.
tomorrow i will try to mesure acmlk, smclk and mclk. In the case that they work at 8 MHz and the P4 output only arrive to some kHz, which could be the problem?
ok, solved problem. i hadn’t set up correctly the clock system, so it was working bad. but i have another question: is it normal that with a 8 mhz clock, i reach only 400 khz in the output? the operation consists, simply, in changing one bit in the p4 from one to zero continously.
The c-compiler probably translates your do {P4OUT++;} while (i==0); into something like:
xyz: add #1,P4OUT;
cmp #0,&i;
jne xyz;
The first instruction takes 4 MCLKs to execute, the second also 4 cycles, and the third 2 cycles. Thus it takes 10 MCLKs to change P4.0 and another 10 MCLKs to change it again. The waveform at P4.0 will be 8MHz/(10+10)=400kHz.
If i was stored in a CPU register (for example R15) rather than RAM, this loop can be slightly faster. cmp #0,R15 takes 1 MCLK instead of 4.
If your application does not need much RAM and Flash, you may want to use MSP430F2012 instead of what you are using now. F2012 is twice as fast, much smaller, and cheaper. If 2% error in frequency is okay for your application, you do not need a crystal. The DCO of F20xx can generate 16MHz all by itself.
For building prototypes, you may want to use ready build “F2012 Target” from TI. I think the list price is 3 boards for US$10.
For bare chips, they also have DIP package which is big but easy to solder by hand. You can request TI to send you up to 3 samples for free. But this depends on where you are.