Hey guys,
I want some basic tutorial for programming In C for MSP430.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hey guys,
I want some basic tutorial for programming In C for MSP430.
Actually i can programming in assembly language but now i want to move on C, so i require some basic tutorial to learn with example code...
I have basic knowledge of C, Now i require some tutorial for MSP programming with C
Jignesh Doshi1 said:is there any one have tutorial?
You shall split your "C for msp430" learning in two parts: 1) C programming 2) msp430 - related C stuff.
1) There are loads of C learning resources around - various youtube video, courses and books. Just use search
2) You shall check workshop materials and videos. C source code examples also will be useful Check web page of the chip you use, for example C source examples of msp430x2 series
Energia could help you get on C track too.
You can use the built-in function _delay_cycles.
It will delay cycles not secods, so you will have to a bit of math first. I don't know your board, but if I searched correctly its 8MHz.
This means it goes through 8 000 000 cycles in 1 secod. If you delay 8 000 000 cycles, you will have waited for 1 second.
For 1 milisecond, divide it by 1000, 8 000 000 / 1 000 = 8 000. So I would say:
1 second: _delay_cycles (8000000); 1 milisec: _delay_cycles(8000);
I hope you can use those in your project.
As We calculate 1us execution time for 1 instruction in 8051 Micro Controller.
Like my crystal is 11.0592 Mhz in 8051, we can calculate 11.0592 / 12 ( its clock pulse) = 0.9216 Mhz and that we have to calculate 1 / 0.9216 = 1.08us / 1 cycle (Execution time).
My question is how can we calculate like this in MSP430F149 Microcontroller.
When you use __delay_cycles(), you do not have to calculate, compiler calculates for you. Just make sure you pass MCLK cycles as an agument.
[edit] For __delay_cycles(NCYCLES) Formula is: NCYCLES = (MCLK_freq/1000)*delay_msecs
I am posting my code in below..
please verify it and give me feedback.
Code is for 4 bit LCD 20*4 lines with MSP430.
data pin is P1.4,1.5,1.6,1.7 and RS pin is P2.3 and EN pin is P2.2.
#include <msp430.h>
#define en BIT2;
#define rs BIT3;
#define rw BIT4;
#define lcd_data_pin P1OUT;
void lcd_ini();
void lcd_command(unsigned char);
void lcd_data(unsigned char);
void lcd_welcome();
void lcd_reset();
void set_clk();
void main(void)
{
unsigned char comm = 0x81,disp = 0x41;
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
set_clk();
P1DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7;
P2DIR |= BIT2 + BIT3;
lcd_ini();
while(1)
{
//lcd_welcome();
P2OUT &= ~BIT3;
P1OUT = (comm & 0xF0);
P2OUT |= BIT2;
__delay_cycles(100);
P2OUT &= ~BIT2;
comm = comm << 4 ;
P1OUT = (comm & 0xF0);
P2OUT |= BIT2;
__delay_cycles(100);
P2OUT &= ~BIT2;
__delay_cycles(500);
P2OUT |= BIT3;
P1OUT = (disp & 0xF0);
P2OUT |= BIT2;
__delay_cycles(100);
P2OUT &= ~BIT2;
disp = disp << 4 ;
P1OUT = (disp & 0xF0);
P2OUT |= BIT2;
__delay_cycles(100);
P2OUT &= ~BIT2;
__delay_cycles(500);
}
}
void set_clk()
{
do
{
IFG1 &= ~OFIFG;
__delay_cycles(10000);
__delay_cycles(10000);
}
while(OFIFG == 1);
BCSCTL1 |= RSEL0 + RSEL1 + RSEL2; //LOW NOMINAL FREQUENCY
BCSCTL2 |= SELS; //XT2 AS SMCLK SOURCE
BCSCTL2 |= SELM1;
BCSCTL2 &= ~SELM0;
BCSCTL2 &= ~DIVS0 + ~DIVS1; //SMCLK DIVIDER 1
return;
}
void lcd_ini()
{
lcd_reset();
lcd_command(0x28);
// __delay_cycles(5000);
lcd_command(0x0C);
__delay_cycles(128000);
}
void lcd_reset()
{
P2OUT &= ~BIT3;
P2OUT |= BIT2;
lcd_command(0x20);
__delay_cycles(128000);
lcd_command(0x20);
_delay_cycles(128000);
lcd_command(0x20);
// _delay_cycles(128000);
}
void lcd_command(unsigned char comm)
{
P2OUT &= ~BIT3;
P1OUT = (comm & 0xF0);
P2OUT |= BIT2;
__delay_cycles(100);
P2OUT &= ~BIT2;
comm = comm << 4 ;
P1OUT = (comm & 0xF0);
P2OUT |= BIT2;
__delay_cycles(100);
P2OUT &= ~BIT2;
__delay_cycles(500);
}
void lcd_data(unsigned char disp) // function to send data on LCD
{
P2OUT |= BIT3;
P1OUT = (disp & 0xF0);
P2OUT |= BIT2;
__delay_cycles(10);
P2OUT &= ~BIT2;
disp = disp << 4 ;
P1OUT = (disp & 0xF0);
P2OUT |= BIT2;
__delay_cycles(10);
P2OUT &= ~BIT2;
_delay_cycles(50);
}
Jignesh Doshi1 said:I want to generate 1Sec, 1mS delay for MSP430F149, so how i have to calculate?
That has nothing to do with using c instead of assembly.
It looks like you've received two suggestions for how to create a 1us delay.
Take Care,
Scott
**Attention** This is a public forum