MSP430 UART receiving a string - TX problem - c

Using MSP430F6736A embedded programming. Code written in Code Composer Studio. I have a problem in my code, but i have no idea what i can do to fix it.
When I compile the program in code composer, works well but UART are sending to PC just ".ø€.ø€.€€x" that means i'm getting only strange charracters
#include "msp430g2553.h"
#include <stdio.h>
float RLDR=0, lux=0, v0=0;
unsigned int ADC10_vetor[16]={0};
unsigned int sum=0;
unsigned char i=0;
unsigned char TX_data[32], tx_index = 0, lux_int =0;
void ini_ucon(void);
void ini_p1_p2(void);
void ini_Timer0(void);
void ini_ADC10(void);
void ini_USCI_UART(void);
void ini_ucon(void){
WDTCTL = WDTPW | WDTHOLD;
DCOCTL = CALDCO_8MHZ;
BCSCTL1 = CALBC1_8MHZ;
BCSCTL2 = DIVS0 + DIVS1; //SMCLK = 1MHz
__enable_interrupt();
}
void ini_p1_p2(void){
P1DIR = 0xFF;
P1OUT = 0x00;
P2DIR = 0xFF;
P2OUT = 0x00;
}
void ini_Timer0(void){
TA0CTL = TASSEL1 + ID1 + ID0 + MC0; //SMCLK = 1MHz/8 = 125000
TA0CCTL1 = OUTMOD0 + OUTMOD1 + OUTMOD2 + OUT;
TA0CCR0 = 62499; //2Hz = 0.5 secs. , TA0CCR0 = 0.5*125000 - 1 = 62499
TA0CCR1 = 31249; //PWM = 50%
}
void ini_ADC10(void){
ADC10CTL0 = ADC10SHT1 + MSC + ADC10ON + ADC10IE;
ADC10CTL1 = INCH0 + INCH2 + SHS0 + ADC10SSEL1 + CONSEQ1;
ADC10AE0 = BIT5; //P1.5 para A5
ADC10DTC0 = 0;
ADC10DTC1 = 16;
ADC10SA = &ADC10_vetor[0];
ADC10CTL0 |= ENC;
}
int main(void){
ini_ucon();
ini_p1_p2();
ini_Timer0();
ini_ADC10();
ini_USCI_UART();
while(1){
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_RTI(void){
ADC10CTL0 &= ~ENC;
soma = 0;
for(i=0;i<16;i++){
sum = sum + ADC10_vetor[i];
}
sum = sum >> 4;
//RLDR = (42625000)/(3*sum)-10000;
//lux = 500/(RLDR/1000);
//v0=5*(sum/(sum+10000));
v0=sum*(3.6/1024);
RLDR = (10000*(5-v0))/v0;
lux = (500/(RLDR/1000));
lux_int = (unsigned char) lux;
sprintf(&TX_data[0], "LUX = %d \n", lux_int);
UCA0TXBUF = TX_data[0];
tx_index++;
ADC10SA = &ADC10_vetor[0];
ADC10CTL0 |= ENC;
}
#pragma vector=USCIAB0TX_VECTOR
__interrupt void RTI_USCI_UART(void)
{
IFG2 &= ~UCA0TXIFG;
if(TX_data[tx_index] == '\0'){
tx_index = 0;
}else{
if(tx_index >= 32){
tx_index = 0;
}else{
UCA0TXBUF = TX_data[tx_index];
tx_index++;
}
}
}
void ini_USCI_UART(void){
UCA0CTL1 |= UCSWRST;
UCA0CTL0 = 0;
UCA0CTL1 = UCSSEL0 + UCSSEL1 + UCSWRST;
UCA0BR0 = 0xA0;
UCA0BR1 = 0x01;
UCA0MCTL = UCBRS1 + UCBRS2;
UCA0STAT = 0;
P1SEL |= BIT2;
P1SEL2 |= BIT2;
UCA0CTL1 &= ~UCSWRST;
IFG2 &= ~UCA0TXIFG;
IE2 |= UCA0TXIE;
}
Does anyone have an idea how to fix this?

Related

Temperature Sensing LED Strip via MSP430G2553 and DHT11 Temperature Sensor

I am a beginner at MCU. I just turned on an LED, and RGB LED. And I will talk about my project briefly:
By using DHT11 Temp. Sensor with MSP430G2553 MCU, LED strip (which I have ws2812b contains 60led on a 1-meter strip) must change its color gradually. Like if the room temp. is 0, LED strip must be blue, but when the room/environment gets hotter, the color has to change gradually to the color red. But it must be like this: blue-> blueish-> reddish-> red.
As I stated before, I am truly a beginner. I did lots of research but all done with Arduino or with different components. I used a code to calculate the temperature by Dr.Selim's youtube channel about DHT11. But I couldn't use th (temperature high) for changing led colors. I truly need help with the code.
Here is what've I tried unskillfully by trying to get 2 different codes work in charm together but have failed.
#include <msp430.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ws2812.h"
volatile int temp[50];
volatile int diff[50];
volatile unsigned int i=0;
volatile unsigned int j=0;
char th_char[5];
char tl_char[5];
char hh_char[5];
char hl_char[5];
volatile int hh = 0;
volatile int hl = 0;
volatile int th = 0;
volatile int tl = 0;
volatile int check = 0;
volatile int checksum = 0;
volatile int dataok;
char temperature[] = "Temperature is: ";
char dot[] = ".";
char celcius[] = " degrees Celcius ";
char humidity[] = "Humidity is: ";
char percent[] = " %\r\n";
void ser_output(char *str);
void main(void);
void gradualFill(u_int n, u_char r, u_char g, u_char b)
int main(void)
{
WDTCTL = WDTPW | WDTHOLD;
BCSCTL1= CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1|BIT2;
P1SEL2 = BIT1|BIT2;
UCA0CTL1 |= UCSWRST+UCSSEL_2;
UCA0BR0 = 52;
UCA0BR1 = 0;
UCA0MCTL = UCBRS_0;
UCA0CTL1 &= ~UCSWRST;
__delay_cycles(2000000);
P2DIR |= BIT4;
P2OUT &= ~BIT4;
__delay_cycles(20000);
P2OUT |= BIT4;
__delay_cycles(20);
P2DIR &= ~BIT4;
P2SEL |= BIT4;
TA1CTL = TASSEL_2|MC_2 ;
TA1CCTL2 = CAP | CCIE | CCIS_0 | CM_2 | SCS ;
_enable_interrupts();
while (1){
if (i>=40){
for (j = 1; j <= 8; j++){
if (diff[j] >= 110)
hh |= (0x01 << (8-j));
}
for (j = 9; j <= 16; j++){
if (diff[j] >= 110)
hl |= (0x01 << (16-j));
}
for (j = 17; j <= 24; j++){
if (diff[j] >= 110)
th |= (0x01 << (24-j));
}
for (j = 25; j <= 32; j++){
if (diff[j] >= 110)
tl |= (0x01 << (32-j));
}
for (j = 33; j<=40; j++){
if (diff[j] >= 110)
checksum |= (0x01 << (40-j));
}
check=hh+hl+th+tl;
if (check == checksum)
dataok = 1;
else
dataok = 0;
//if you get an error with ltoa, try: 'ltoa(th,th_char,10)'
//check article:
//https://software-dl.ti.com/ccs/esd/documents/sdto_cgt_handling_changes_in_ltoa.html
ltoa(th,th_char, 10);
ltoa(tl,tl_char, 10);
ltoa(hh,hh_char, 10);
ltoa(hl,hl_char, 10);
ser_output(temperature); ser_output(th_char); ser_output(dot); ser_output(tl_char); ser_output(celcius);
ser_output(humidity); ser_output(hh_char); ser_output(dot); ser_output(hl_char); ser_output(percent);
__delay_cycles(1000000);
WDTCTL = WDT_MRST_0_064;
}
if (CALBC1_16MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
// configure clock to 16 MHz
BCSCTL1 = CALBC1_16MHZ; // DCO = 16 MHz
DCOCTL = CALDCO_16MHZ;
// initialize LED strip
initStrip();
while (1) {
gradualFill(NUM_LEDS, 0x00, 0xFF, 0x00); // green
gradualFill(NUM_LEDS, 0x00, 0x00, 0xFF); // blue
gradualFill(NUM_LEDS, 0xFF, 0x00, 0xFF); // magenta
gradualFill(NUM_LEDS, 0xFF, 0xFF, 0x00); // yellow
gradualFill(NUM_LEDS, 0x00, 0xFF, 0xFF); // cyan
gradualFill(NUM_LEDS, 0xFF, 0x00, 0x00); // red
}
}
}
void gradualFill(u_int n, u_char r, u_char g, u_char b){
int th;
for (th = 0; i < n; th++){ // n is number of LEDs
setLEDColor(th, r, g, b);
showStrip();
_delay_cycles(1000000); // lazy delay
}
}
#pragma vector = TIMER1_A1_VECTOR
__interrupt void Timer_A1(void){
temp[i] = TA1CCR2;
i += 1;
TA1CCTL2 &= ~CCIFG ;
if (i>=2) diff[i-1]=temp[i-1]-temp[i-2];
void ser_output(char *str){
while(*str != 0){
while (!(IFG2&UCA0TXIFG));
UCA0TXBUF = *str++;
}
}

STM8L051F3 GPIO Initial Config

I am programming the STM8L051F3 processor for a simple application in which it is only necessary to control all GPIO.
The problem is that I can't control the GPIOC pin 0.
I just configured the GPIO registers. Is any other startup necessary?
I also tried to use stm8cubemx on Ubuntu. Software that I found useless because it generates an ioc8 file that has no information on how to use it in Ubuntu.
#include <stdint.h>
#include <string.h>
#include "stm8l.h"
void config_gpio(){
//76543210
PA_DDR = 0b00000000;
PA_CR1 = 0xff;
PA_CR2 = 0x00;
//76543210
PB_DDR = 0b00100110;
PB_CR1 = 0xff;
PB_CR2 = 0x00;
//76543210
PC_DDR = 0b01110011;
PC_CR1 = 0xff;
PC_CR2 = 0x00;
//76543210
PD_DDR = 0b00000000;
PD_CR1 = 0xff;
PD_CR2 = 0x00;
PA_ODR = 0x00;
PB_ODR = 0x00;
PC_ODR = 0x00;
PD_ODR = 0x00;
return;
}
void delay(unsigned long delay){
unsigned long i = 0;
for(i = 0; i < delay; i++) {}
return;
}
void rotate_left(int steps){
int n=0;
for(n=0; n<steps; n++){
//PC_DDR = 0b01110011;
//76543210
PC_ODR = 0b01100001;
delay(100);
PC_ODR = 0b00110001;
delay(100);
PC_ODR = 0b00010011;
delay(100);
PC_ODR = 0b01000011;
delay(100);
}
return;
}
void rotate_right(int steps){
int n=0;
for(n=0; n<steps; n++){
//PC_DDR = 0b01110011;
//76543210
PC_ODR = 0b01000011;
delay(100);
PC_ODR = 0b00010011;
delay(100);
PC_ODR = 0b00110001;
delay(100);
PC_ODR = 0b01100001;
delay(100);
}
return;
}
int main() {
config_gpio();
do {
delay(7777);
rotate_right(100);
rotate_left(100);
} while(1);
}
Terminal cmd.
Compile
sdcc -lstm8 -mstm8 --opt-code-size --std-sdcc99 --nogcse --all-callee-saves --debug --verbose --stack-auto --fverbose-asm --float-reent --no-peep -I./ -I./STM8S_StdPeriph_Driver/inc -D STM8L051 ./main.c
Prog
stm8flash -c stlinkv2 -p stm8l051f3 -s flash -w main.ihx
PC0 and PC1 only works as open drain in stm8l051f3.

Atmega16 USART initialization

I can't for the life of me find why this code is not working to send a byte to my computer terminal window. It receives and works fine when I omit that line though. Receiving and sending also work seperately. Please note that this is over rs485 so I must disable TXEN if I want anything received to make it through the line driver ic.
#define F_CPU 7800000UL // 7.8 MHz
#include <avr/io.h>
//#include <util/delay.h>
#include <avr/interrupt.h>
#include <math.h>
#define USART_BAUDRATE 57600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
volatile unsigned int value = 0;
volatile int recievearray[4] = {0,0,0,0};
volatile int x=0;
void USART_Init(void)
{
DDRD = 0x7E; //you have to assign TXEN as output for some reason
//PORTD = 0xFF;
UCSRB = 0;
//Put the upper part of the baud number here (bits 8 to 11)
UBRRH = (unsigned char) (BAUD_PRESCALE >> 8);
//Put the remaining part of the baud number here
UBRRL = (unsigned char) BAUD_PRESCALE;
//PORTD = 2;
// ASYNCRONOUS
UCSRC = (0 << UMSEL);
//Enable the receiver and transmitter
UCSRB = (1 << RXEN) | (1 << TXEN) | (0 << UCSZ2) | (1<<RXCIE);
//Set 1 stop bits and data bit length is 8-bit
UCSRC = (1 << URSEL) | (0 << USBS) | (3 << UCSZ0);
//no parity
UCSRC |= (0 << UPM1);
UCSRC |= (0 << UPM0);
}
void USART_SendByte(uint8_t u8Data)
{
UCSRB = (1 << TXEN) | (1<<TXCIE);
// Wait until last byte has been transmitted
while((UCSRA &(1<<UDRE))==0);
UDR = u8Data;
UCSRB = (0 << TXEN) | (0<<TXCIE);
}
void Led_init(void){
DDRC = 0xFF;
}
ISR(USART_RXC_vect)
{
//PORTC = 0xFF;
while((UCSRA &(1<<RXC)) == 0);
value = UDR;
if (x < 4)
{
recievearray[x++] = value;
}
else
{
x = 0;
recievearray[x++] = value;
PORTC = 0x00;
}
}
int main(void)
{
USART_Init();
sei();
Led_init();
//PORTC = 0xFF;
for(;;)
{
if (recievearray[0] + recievearray[1] + recievearray[2] + recievearray[3] > 100)
{
PORTC = 0xFF;
USART_SendByte(value);
}
//_delay_ms(1000);
}
}

An issue that might lay in the code which I don't see (using a ledstrip) Atmel Studio 6.1

I am using Atmel Studio 6.1 and this is the code that I use, beside this i also got another code called "ledstrip.h". Which basically is an 1 dimensional array.
This is something for my study.
I am trying to send 12 bytes through a 'ledstrip'
code:
#define SPI_SS_bm PIN4_bm
#define SPI_MOSI_bm PIN5_bm
#define SPI_MISO_bm PIN6_bm
#define SPI_SCK_bm PIN7_bm
#define MIN_VALUE 0.4
#define F_CPU 32000000UL
#define P 128
#define FOO 0
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "ledstrip.h"
void spi_init(void);
uint8_t spi_transfer(uint8_t data);
const uint8_t *ptr = image;
uint8_t num[12];
int line;
int g;
int data;
int i;
void row (const uint8_t *j)
{
uint32_t row2 = 0;
for(g = 0; g < 4; g++)
{
for(int i = 0; i < 8; i++)
{
row2 = (row2 << 3) | pgm_read_byte(j);
j++;
}
num[3*g] = (uint8_t) (row2 >> 16); //put bytes in the right order
num[3*g+1] = (uint8_t) (row2 >> 8);
num[3*g+2] = (uint8_t) (row2);
}
}
void spi_initialisation(void)
{
PORTC.DIRSET = SPI_SCK_bm|SPI_MOSI_bm|SPI_SS_bm;
PORTC.DIRCLR = SPI_MISO_bm;
SPIC.CTRL = (!SPI_CLK2X_bm) | // no double clock speed
SPI_ENABLE_bm | // SPI enable
!SPI_DORD_bm | // data order, MSB first
SPI_MASTER_bm | // master
SPI_MODE_0_gc | // mode 0
SPI_PRESCALER_DIV4_gc; // Presc. 4 (#2 MHz,500kHz)
}
void spi_init(void)
{
PORTC.DIRSET = PIN7_bm|PIN5_bm|PIN4_bm; // 7: MOSI 5: SCK
PORTC.OUTCLR = PIN4_bm; // 4: latch enable
PORTD.DIRSET |= PIN3_bm|PIN1_bm; // 3: MOSI 1: SCK
USARTC1.BAUDCTRLA = 0; // baud rate FCPU/2
USARTC1.BAUDCTRLB = 0; //
USARTC1.CTRLC = USART_CMODE_MSPI_gc; // SPI mode
USARTC1.CTRLA = 0; // no interrupts
USARTC1.CTRLB = USART_TXEN_bm; // enable transmit
USARTD0.BAUDCTRLA = 0; // baud rate FCPU/2
USARTD0.BAUDCTRLB = 0; //
USARTD0.CTRLC = USART_CMODE_MSPI_gc; // SPI mode
USARTD0.CTRLA = 0; // no interrupts
USARTD0.CTRLB = USART_TXEN_bm; // enable transmit
}
void init_timer_phi(void)
{
TCE0.CTRLA = TC_CLKSEL_DIV8_gc; // prescaling P
TCE0.CTRLB = TC_WGMODE_NORMAL_gc; // normal mode
TCE0.INTCTRLA = TC_OVFINTLVL_LO_gc;
TCE0.PER = 1440; // t = PER*FCPU/P =
// FCPU*P/32M = 1 us
}
ISR (TCE0_OVF_vect)
{
ptr = ptr +32;
if(ptr >= image + 32 * 360)
{
ptr = image;
} // send bytes to ledstrip
}
void init_inputcapture(void)
{
PORTC.PIN2CTRL = PORT_ISC_FALLING_gc;
PORTC.DIRCLR = PIN2_bm; // Pin 2 is input
EVSYS.CH0MUX = EVSYS_CHMUX_PORTC_PIN2_gc; // Select PC2 as input
// to event channel 0
TCC0.CTRLD = TC_EVACT_CAPT_gc | // Event capture
TC_EVSEL_CH0_gc; // for Channel 0
TCC0.CTRLB = TC0_CCAEN_bm; // Enable Inp. Capt. Ch. A
TCC0.CTRLA = TC_CLKSEL_DIV256_gc; // Start timer
TCC0.INTCTRLB = TC_CCAINTLVL_LO_gc; // Set Interr. level Ch. A
TCC0.PER = 0xFFFF;
}
ISR(TCC0_CCA_vect)
{
uint16_t v;
v = TCC0.CCA;
if (v > MIN_VALUE)
{ // skip if measured value is too small
line = 0; // reset image
TCE0.PER = (v * 360/(F_CPU / P)); // calculate periode
TCC0.CTRLFSET = TC_CMD_RESTART_gc; // restart input capture
}
}
void spi_write_byte(uint8_t data)
{
PORTC.OUTCLR = SPI_SS_bm;
spi_transfer(data);
PORTC.OUTSET = SPI_SS_bm;
}
uint8_t spi_read_byte(void)
{
uint8_t data;
PORTC.OUTCLR = SPI_SS_bm;
data = spi_transfer (FOO);
PORTC.OUTSET = SPI_SS_bm;
return data;
}
int main(void)
{
uint8_t i = 0;
spi_init();
PORTC.DIRSET = PIN0_bm;
PMIC.CTRL |= PMIC_LOLVLEN_bm;
sei();
PORTC.DIRSET = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
while(1)
{
SPIC.DATA = i; // send i
while( ! (SPIC.STATUS & (SPI_IF_bm)) ); // wait until send
PORTC.OUTSET = PIN0_bm; // store
PORTC.OUTCLR = PIN0_bm;
_delay_ms(100);
i++;
}
}
Okay, so I got this code with no errors and warnings.
But when I run the program on my microcontroller ATXMega128A4U, I don't get the intended output.
There is a possibility that I didn't connect the pins of my 'ledstrip' in the right way with the pins of the microcontroller.
But if we consider that I did it like I intended to, the problem should be in the code.
The intended signal is 12 bytes long and with that I can turn RGB LED's on or off.
The RGB LED's are put together in a so called 'ledstrip'
Now what I'm getting is that some of the RGB LED's turn on and are Red Green or Blue.
What i should be getting is something like a chasing RGB LED row.
So you would see the LED's turn on or off at a certain frequency.

Compiling a C program for a pic microcontroller - error message

I have a C program that I am trying to put on my pic but I get the error "3.17 can't open include file "main.h": No such file or directory". This is my first time ever attempting to program a pic.
Here is the program I am trying to put on it.
#include <htc.h>
#include <stdio.h>
#include "main.h"
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_HI & LVP_OFF);
#define _XTAL_FREQ 4000000
int main()
{
int t = 0;
char outStr[8];
char ch;
init();
Lcd_PutStr(0, 0, "Option 1");
Lcd_PutStr(1, 0, "Spec Analyzer");
while (1)
{
// do stuff here
}
}
int init()
{
// Hardware
OSCCON = 0x6A; // 4 MHz oscillator
PORTA = 0; // Clear Port A
LATA = 0;
PORTB = 0; // Clear Port B
LATB = 0;
PORTC = 0; // Clear Port C
LATC = 0;
ANSELA = 0; // Set Port A to digital
ANSELB = 0; // Set Port B to digital
ANSELC = 0; // Set Port C to digital
TRISB = 0; // Set port B to output
TRISC = 0; // Set port C to output
// LCD display
Lcd_Init();
return 0;
}
void ENPulse()
{
LATC |= 0x20;
__delay_us(1);
LATC &= 0xdf;
}
void Lcd_Init()
{
LATC = 0;
__delay_ms(40);
LATC = 0x03;
ENPulse();
__delay_us(37);
Lcd_Cmd(0x28);
__delay_us(37);
Lcd_Cmd(0x28);
__delay_us(37);
Lcd_Cmd(0x0C);
__delay_ms(2);
Lcd_Cmd(0x01);
__delay_us(37);
}
void Lcd_Cmd(byte cmd)
{
LATC = cmd >> 4;
ENPulse();
LATC = cmd & 0x0F; // clear RS (LATC,4)
ENPulse();
__delay_us(100);
}
void Lcd_Data(byte data)
{
LATC = (data >> 4) | 0x10; // set RS (LATC,4)
ENPulse();
LATC = (data & 0x0F) | 0x10; // set RS (LATC,4)
ENPulse();
__delay_us(100);
}
void Lcd_PutStr(int row, int col, char* str)
{
int pos;
// use row and column=-1 for no positioning, i.e. position where last ended
if (row >= 0 && col >= 0)
{
if (row > 0)
row = 0x40; // row 1
else
row = 0; // row 0
pos = row | col | 0x80; // 0x80 is cmd for positioning cursor
Lcd_Cmd(pos);
}
while (*str)
{
Lcd_Data(*str);
str++;
}
}
I am using MPLab and Hi-Tech C with a Pickit3. I based this code off of a sample given to me. I do not know what the purpose of the main.h file is. Any help is appreciated.
After reviewing your errors, you may be able to run the code without main.h. It looks like main.h mostly prototypes the functions at the bottom of the file. Try putting this code between the main and the #define:
int init();
void ENPulse();
void Lcd_Init();
void Lcd_Cmd(byte cmd);
void Lcd_Data(byte data);
void Lcd_PutStr(int row, int col, char* str);
This should at lest reduce most of your errors. It may fix it completely. The idea is that when the compiler first encounters theses functions it doesn't know what they are and it throws errors before it can find them. If there are still errors let me know and I'll try and help more.
Try this code:
#include <htc.h>
#include <stdio.h>
//#include "main.h"
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_HI & LVP_OFF);
#define _XTAL_FREQ 4000000
int init()
{
// Hardware
OSCCON = 0x6A; // 4 MHz oscillator
PORTA = 0; // Clear Port A
LATA = 0;
PORTB = 0; // Clear Port B
LATB = 0;
PORTC = 0; // Clear Port C
LATC = 0;
ANSELA = 0; // Set Port A to digital
ANSELB = 0; // Set Port B to digital
ANSELC = 0; // Set Port C to digital
TRISB = 0; // Set port B to output
TRISC = 0; // Set port C to output
// LCD display
Lcd_Init();
return 0;
}
void ENPulse()
{
LATC |= 0x20;
__delay_us(1);
LATC &= 0xdf;
}
void Lcd_Init()
{
LATC = 0;
__delay_ms(40);
LATC = 0x03;
ENPulse();
__delay_us(37);
Lcd_Cmd(0x28);
__delay_us(37);
Lcd_Cmd(0x28);
__delay_us(37);
Lcd_Cmd(0x0C);
__delay_ms(2);
Lcd_Cmd(0x01);
__delay_us(37);
}
void Lcd_Cmd(byte cmd)
{
LATC = cmd >> 4;
ENPulse();
LATC = cmd & 0x0F; // clear RS (LATC,4)
ENPulse();
__delay_us(100);
}
void Lcd_Data(byte data)
{
LATC = (data >> 4) | 0x10; // set RS (LATC,4)
ENPulse();
LATC = (data & 0x0F) | 0x10; // set RS (LATC,4)
ENPulse();
__delay_us(100);
}
void Lcd_PutStr(int row, int col, char* str)
{
int pos;
// use row and column=-1 for no positioning, i.e. position where last ended
if (row >= 0 && col >= 0)
{
if (row > 0)
row = 0x40; // row 1
else
row = 0; // row 0
pos = row | col | 0x80; // 0x80 is cmd for positioning cursor
Lcd_Cmd(pos);
}
while (*str)
{
Lcd_Data(*str);
str++;
}
}
int main()
{
int t = 0;
char outStr[8];
char ch;
init();
Lcd_PutStr(0, 0, "Option 1");
Lcd_PutStr(1, 0, "Spec Analyzer");
while (1)
{
// do stuff here
}
}

Resources