Trying to understand registers on MSP430G2553

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
@mckenney Your post cleared a lot of my confusion regarding the Timer A module. I can now see some things come together and understand the lines of code in the program I posted. Though I doubt I could write one on my own, yet, though.

This is the (CCR0) timer ISR. Nobody cares what the function name is. __interrupt assures the proper entry/exit sequence. The _VECTOR name is what puts it into the proper spot in the interrupt vectors up at the top of memory.
I'm still struggling with the pragma vector thing part. I can't seem to find the "__interrupt" anywhere in the msp430g2553.h file, or the User Guide, or even the data sheet. Where does it come from? I can guess that it is something the code tells the compiler about the function, that it's an interrupt function, and the double underscore is just to hinder name confusion.
On page 25 of the guide is a memory map, and from 0FFE0h to 0FFFFh is the Vector Table. This just means, that all pragma vectors must point to within these addresses?

@MrChips That clears things up a whole lot, regarding capture/compare :)
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
Hehe...well If I haven't covered all my tracks up til this point (and I haven't..), I don't mind laying a lid on #pragma for now, if I don't have to.

EDIT: I take no responsibility for the wording of my sentences from this point on and about 4 hours ahead.
 
Last edited:

mckenney

Joined Nov 10, 2018
125
@mckenney Though I doubt I could write one on my own, yet, though.
Maybe.That example, in a few lines of code, gives you a template for a program which does some action periodically.Blinking an LED gets old pretty quickly (no it doesn't really :)-))), but maybe you want to check the temperature once per second.You don't want to make your ISR too big, but you can do some things there.
I'm still struggling with the pragma vector thing part.
TIMER0_A0_VECTOR is a #define -- a small integer which is used as an index into the Vector Table described in Table 5 of the data sheet (SLAS735J).

The "#pragma vector=" line itself tells the compiler "put the next function you see into the Vector Table". The compiler and linker cooperate to put its address in the right spot in the final image.
I can't seem to find the "__interrupt" anywhere in the msp430g2553.h file, or the User Guide, or even the data sheet. Where does it come from?
__interrupt is an extended C keyword recognized by the compiler. It is a C declaration "storage" attribute, analogous to "static", that tells the compiler to (among other things) generate a return-from-interrupt rather than a return-from-call instruction at the end.It's described in the CCS cc manual (SLAU132R) section 5.7.2, which goes into pretty much all of its effects.

"#pragma vector=" and "__interrupt" are separate things, but are pretty much always used together.
the double underscore is just to hinder name confusion.
The "__" is part of the keyword. Since it's an extension to the language, the __ is mandated by the C standard to, as you say, avoid naming conflicts among extensions.
 

MrChips

Joined Oct 2, 2009
35,017
Could anyone of you come up with an exercise for suitable for the level I am at (based on this thread)?
Possibilities abound.
Do the following in stages:

1. Turn on an LED only when a button is pressed.
2. Turn on an LED when the button is pressed and released. Turn off the LED when the button is pressed a second time.
3. Do 1 and 2 as above, but instead of a steady light, make the LED flash at a rate of 2Hz.
4. Press once, LED comes on. Press again LED flashes at 2Hz. Press again and the LED goes off. (My bike helmet has a red LED that does this.)

There is more to come.
 

mckenney

Joined Nov 10, 2018
125
Could anyone of you come up with an exercise for suitable for the level I am at (based on this thread)?
Up there somewhere you had a program which displayed a running counter in a bank of LEDs. I recall whining about your software delay for-loop.

You could do the same thing based on your "periodic function" program from github. You'll quickly get impatient with once-per-second updates, so adjust the timer registers to speed it up.

And I will have done my small part in eradicating software delay for-loops from the Earth :)-)).
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
@MrChips I'm on it! Just need to finish my drooling over a Win98 retro gaming PC on the tube.

@mckenney I will do Chips's assignments, using your delay suggestion ;)

BtwOT; My hobbies tend to come and go, but if this one sticks, should I go with an oscilloscope, or a logic analyzer? Let's say I can only pick one.

BtwOT2; NOT to turn this into an tool thread, but are analogue oscilloscopes useful when playing with microcontrollers? Reason I ask is because there are a whole bunch of them for sale where I live. Only a couple of digital.
 
Last edited:

MrChips

Joined Oct 2, 2009
35,017
If you're on a budget, go with what you can afford.
However, it really depends on the make and model of the scope.
1) Max frequency matters, analog or digital.
2) It is hard to see low repetition rates on an analog scope.
3) A digital scope allows you to capture a single event.
4) A scope is more useful than a logic analyzer.
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
@MrChips I will hold on with the scope for now, but thanks for the tips.

I give up. I've been trying to get this to work all day now, but I have to reside to seeking some help. It's the first mission of 4.

The "P1OUT = BIT3; //Resistor to pullup???" I found/copied, but I don't understand it. That's just supposed to light up BIT3 and keep it lit, right?? It just doesn't do that.

Code:
int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;    // stop watchdog timer
    P1DIR = BIT1 + BIT4;    //Set LEDs on breadboard to toggle
    P1REN = BIT3;           //Enable pullup resistor for the S2 button
    P1OUT = BIT3;           //Resistor to pullup???


    while(1)
    {

        if((P1IN & BIT3 ))
        {
            P1OUT ^= BIT1 + BIT4;     //Blink two LEDS on the breadboard
        }

    }
    return 0;
}
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
How do I know what is in the P1IN/OUT register? Should I just assume that it's all 0, if I haven't stated otherwise?

I am trying to write them all down on a paper, so I can get used to working with bits, as I find it a bit difficult to visualize the registers.

And why does "P1DIR = BIT1 + BIT4" turn on the LEDs? :confused: EDIT: A bit too fast here. It doesn't turn it on, but when I stepped through the program, the LEDs lit up at that line, then turned off again. Never noticed that before, I guess.
 
Last edited:

MrChips

Joined Oct 2, 2009
35,017
P1DIR does not turn on LEDs.
P1DIR switches the port from input (when P1DIR bit is 0) to output (when P1DIR bit is 1).
(Note that this is opposite from Microchip PIC MCUs.)

Use P1OUT to turn on/off the output pin.

P1IN is an 8-bit port/register. It will hold the current status on the 8 I/O pins.
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
The problem is I want to write IF(P1 BIT 3 = 1) THEN DO STUFF. But I dont know how to express that in CCS. Havent tried with your union yet, because I wanna learn to work with the standard library, so to speak.
 

MrChips

Joined Oct 2, 2009
35,017
You were almost there.
You need to say:

IF something
DO this
else
DO that

Code:
int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;    // stop watchdog timer
    P1DIR = BIT1 + BIT4;    //Set LEDs on breadboard to toggle
    P1REN = BIT3;           //Enable pullup resistor for the S2 button
    P1OUT = BIT3;           //Resistor to pullup???


    while(1)
    {

        if (P1IN & BIT3 )
        {
            P1OUT &= ~(BIT1 + BIT4);   
        }
      else
        {
            P1OUT |= BIT1 + BIT4;  
        }

    }
    return 0;
}
Later I will show you how you should start thinking about making your code portable.
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
Oh man, that is very close to what I've got so far. Im on my phone, cant post it right now.

Does only 1 bit have to come out true in the IF statement for it to be true? If I understand it correctly, it performs an & operation on P1IN reg. value and BIT3 value, and if the value is say 00001000, then condition is true.
 
Top