STM8L051F3 GPIO Initial Config - c

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.

Related

MSP430 UART receiving a string - TX problem

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?

UART only receiving FF and FE

I am currently working on transmitting from a PIC18F4620, through a FT232, to CoolTerm. I am currently only receiving FF and FE from the PIC though. I was wondering why this may be the case. The Rx - TX are correctly switched, the cable connecting them appears to be secure. The only issue I can think of is the baud rate would be incorrect, But looking at the datasheet I don't believe it is. any insight would be greatly appreciated. (I arbitrarily chose 51 as my test number. any number or letter would work).
Circuit
const unsigned char MSG0[] = "Transmitting... ";
const unsigned char MSG1[] = "Sent:";
const unsigned char MSG2[] = "TEST:";
// Subroutine Declarationsb
#include <pic18.h>
// Subroutines
#include "lcd_portd.c"
#include <delays.h>
#include <plib.h>
#include <stdint.h>
void UART_TX_Init(void)
{
BRG16 = 0;
BRGH = 1; // Set For High-Speed Baud Rate
SPBRG = 64; // Set The Baud Rate To Be 9600 bps
//--[ Enable The Ascynchronous Serial Port ]--
SYNC = 0;
SPEN = 1;
//--[ Set The RX-TX Pins to be in UART mode (not io) ]--
TRISC6 = 1; // As stated in the datasheet
TRISC7 = 1; // As stated in the datasheet
TXEN = 1; // Enable UART Transmission
}
void UART_Writes(uint8_t data)
{
while(!TRMT);
TXREG = data;
}
// Main Routine
void main(void)
{
UART_TX_Init();
unsigned int result = 0;
unsigned int i;
uint8_t data = 51;
TRISA = 0;
TRISC = 0;
TRISB = 0;
TRISD = 0;
TRISE = 0;
TRISA = 0;
TRISB = 0x00;
PORTC = 0;
PORTD = 0;
PORTE = 0;
ADCON1 = 0x0F;
LCD_Init(); // initialize the LCD
LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]);
Wait_ms(100);
LCD_Move(1,0); for (i=0; i<5; i++) LCD_Write(MSG1[i]);
while(1) {
Wait_ms(1000);
UART_Writes(data);
LCD_Move(1,5); LCD_Out(data,3,0);
}
}
CoolTerm
What is your clock frequency and how did you configured your crystal oscillator setup ? please share your clock setting and configuration.
If clock setting is fine then calculate the correct baud-rate and try.
void UART_Writes(uint8_t data)
{
while(!TRMT)
{
//put Nop() and try
}
TXREG = data;
}

PIC16F883 Led Blink

I need to program a PIC16F883 to blink / light up LED's at the same time. The oscillator is running at 3,2768, and I'm using TIMER0 to help me with the timing.
Right now, I have a prescaler set to 1:256, so I get an interrupt every 50ms, and I have a variable that is calculated from that, to display how many seconds has gone.
If the input is changed, the seconds variable is of course reset again.
Here is the assignment from my teacher:
If Input is 0 (Closed):
The Red And The Green LED should be turned on at the same time for 15 seconds. After this the green LED should be turned
off completely, and the red LED should blink every fifth second for 10 minutes
If input is 1 (Opened):
The red LED should be turned off completely, and the green LED should be turned on for 10 minutes, and after that
it should be turned off too.
My timer is working fine. I have tested that. The program runs fine too and keeps the 2 LED's turned off for 15 seconds, then turns them off, but my red LED isn't blinking. I have been sitting at my desk all day desperately trying to find the error in my code.
Picture of the print:
Here is my C Code. I am using MPLab and the HI-TECH C compiler :)
#include <pic.h>
//Variabler
int B = 0; //Definerer variablen B, used as a flag
int A = 0; //Definerer veriablen A, used as a flag
int E = 0;
int savedstatus = 1; //Definere variablen savedstatus used to check last status for input
int millicounter = 0; //Variabel to calculate seconds
int sec = 0; //Variabel holding seconds gone
int count = 0; //For counting seconds passed, used in input0 subroutine
int onesec = 0; //Used to counting seconds for blinking LED in input0 subroutine
int scount = 0;
//Variabler slut
void interrupt jesper(void)
{
T0IF = 0x00;
TMR0 = 96;
millicounter++;
if(millicounter == 20)
{
sec++;
millicounter = 0;
}
}
//Subrutines
void input0()
{
if(sec<=15 && E==0)
{
PORTA = 0x21;
}
else if(A==0)
{
scount = 0;
sec = 0;
count = sec;
A = 1;
E = 1;
}
else if(sec<=600 && sec>count)
{
count++;
if((scount+5)>=count)
{
if(B==0)
{
onesec = sec;
B = 1;
PORTA = 0x01;
}
else if(sec>onesec)
{
PORTA = 0x00;
B = 0;
scount = count;
scount;
}
else
{
PORTA = 0x01;
}
}
else
{
PORTA = 0x00;
}
}
else PORTA = 0x00;
}
void input1()
{
if(sec<=600)
{
PORTA = 0x20;
}
else
{
PORTA = 0x00;
}
}
//Subrutines over
int main(void)
{
TRISA = 0x00; //Sets all A-PORTS to output
TRISB = 0x01; //Sets all PORTB to output with the exception of BIT0
TRISC = 0x00; //Sets All PORTC to output
ANSEL = 0x00; //Disable Analog ports
ANSELH = 0x00; //Disable Analog ports
//Timer Config
PSA = 0x00;
PS0 = 0x01;
PS1 = 0x01;
PS2 = 0x01;
TMR0 = 0x60;
GIE = 0x01;
T0IE = 0x01;
T0IF = 0x00;
T0CS = 0x00;
//Timer Config Over
while(0x01)
{
if(savedstatus != RB0)
{
savedstatus = RB0;
sec = 0;
E = 0;
A = 0;
}
if(savedstatus == 1)
{
input1();
}
else
{
input0();
}
}
}
I really hope that you can help me here :)
Here is my flowchart for the subroutine input0 where my problem is. Btw some of my variables have different names in the code itself, but it shouldn't be hard to see.
Your main() calls input0() as often as possible. When input0() is in the flashing state it uses the conditional sec > count. I suspect that your intention is that input0() should only change the LED state at intervals of a second. But then on the else side of that conditional you turn both LEDs off. This else side is executing many times because main() is calling input0() so often. Try deleting the else condition where you turn the LEDs off.
void input0()
{
<snip>
else if(sec<=600 && sec>count)
{
<snip>
}
else PORTA = 0x00; // <-- delete this line so you're not always turning the LED off
}

Controlling a DC motor with a PIC 18 PWM

Im using the PIC 18 microcontroller to control the speed of a DC Motor using PWM. I have managed to get it to spin using the code below. And I have tested that my H-Bridge is 100% functional.
However, when I switch on my circuit, 12V to the Motor and 5V to the logic, And I send a command to the circuit using my RS232 communication module, (which I have tested and it recieves and transmits correctly), the program resets and the current on the bench power supply falls to 0A. Sometimes the motor jerks slighty, almost as if its trying to spin, but then stops.
Any ideas where I could be going wrong?
/*
* File: serial.c
* Author: Chris Lombaard
*
* Created on September 13, 2013, 2:39 PM
*/
#pragma config FOSC = INTIO7 // Oscillator Selection bits (Internal oscillator block, CLKOUT function on OSC2)
#pragma config PLLCFG = OFF // 4X PLL Enable (Oscillator used directly)
#pragma config PRICLKEN = ON // Primary clock enable bit (Primary clock is always enabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
#pragma config PWRTEN = OFF // Power-up Timer Enable bit (Power up timer disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 190 // Brown Out Reset Voltage bits (VBOR set to 1.90 V nominal)
#pragma config WDTEN = OFF // Watchdog Timer Enable bits (WDT is always enabled. SWDTEN bit has no effect)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
#pragma config CCP2MX = PORTC1 // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
#pragma config CCP3MX = PORTB5 // P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)
#pragma config HFOFST = ON // HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status)
#pragma config T3CMX = PORTC0 // Timer3 Clock input mux bit (T3CKI is on RC0)
#pragma config P2BMX = PORTD2 // ECCP2 B output mux bit (P2B is on RD2)
#pragma config MCLRE = EXTMCLR // MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
#pragma config CP0 = OFF // Code Protection Block 0 (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection Block 1 (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection Block 2 (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection Block 3 (Block 3 (006000-007FFFh) not code-protected)
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
#pragma config WRT0 = OFF // Write Protection Block 0 (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (006000-007FFFh) not write-protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config EBTR0 = OFF // Table Read Protection Block 0 (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
#include <P18F45K22.h>
#include <xc.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#define length(x) (sizeof(x) / sizeof(x[0])) //Length of array
void writeUART(const unsigned char string[]);
void setup(void);
void inputCheck(void);
void stepF(void);
void stepB(void);
unsigned char buffer[8] = {'\0'};
unsigned char prevPos = 0;
unsigned char currPos = 0;
unsigned char bufferLength;
unsigned char direction = 0;
unsigned char speed = 0;
unsigned char isSetup = 0;
char main(void){
if(isSetup == 0){
setup();
writeUART("ERD 320 Practical 2 ----- Group 1\n");
writeUART("JC Lombaard - 11028786\n");
writeUART("VFDC Henriques - 11100232\n");
writeUART("William Reeler - 11228866\n");
writeUART("FAN POSITION: 1");
PORTDbits.RD1 = 1;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 0;
PORTAbits.RA6 = 0;
}
while(1){
if(PORTEbits.RE1 == 1)
stepB();
if(PORTEbits.RE0 == 1){
writeUART("\nFan calibrated!\n");
break;
}
}
bufferLength = 0;
while (1);
return (EXIT_SUCCESS);
}
void interrupt high_priority isr_high(void) {
if(PIR1bits.RC1IF == 1){
if(RCREG1 == '\n'){
buffer[bufferLength++] = '\0';
bufferLength = 0;
inputCheck();
}else{
buffer[bufferLength++] = RCREG1;
PIR1bits.RC1IF = 0;
}
}
}
void interrupt low_priority isr_low(void){
PIR1bits.TMR2IF = 0;
TMR2 = 0;
}
void inputCheck(void) {
const unsigned char commands[11][3] = {"S0", "S1", "S2", "S3", "S4", "P1", "P2", "P3", "P4", "R", "F"};
unsigned char choice = 0;
for(; choice < 11; choice++)
if (strcmp(buffer, commands[choice]) == 0){
break;
}
switch(choice){
case 0:
writeUART("FAN SPEED: 0% DC");
PORTA = 0b00111111;
speed = 0;
if(direction == 0){
CCPR1L = 0x00;
}else{
CCPR2L = 0x00;
}
break;
case 1:
writeUART("FAN SPEED: 10% DC");
PORTA = 0b00000110;
speed = 0b01101110 ;
if(direction == 0){
CCPR1L = 0b11010000 ;
__delay_ms(100);
CCPR1L = 0b01101110 ;
}else{
CCPR2L = 0b11010000 ;
__delay_ms(100);
CCPR2L = 0b01101110 ;
}
break;
case 2:
writeUART("FAN SPEED: 30% DC");
PORTA = 0b01011011;
speed = 0b10001100;
if(direction == 0){
CCPR1L = 0b11010000;
__delay_ms(100);
CCPR1L = 0b10001100;
}else{
CCPR2L = 0b11010000 ;
__delay_ms(100);
CCPR2L = 0b10001100 ;
}
break;
case 3:
writeUART("FAN SPEED: 60% DC");
PORTA = 0b01001111;
speed = 0b10101101;
if(direction == 0){
CCPR1L = 0b11010000 ;
__delay_ms(100);
CCPR1L = 0b10101101 ;
}else{
CCPR2L = 0b11010000 ;
__delay_ms(100);
CCPR2L = 0b10101101 ;
}
break;
case 4:
writeUART("FAN SPEED: 90% DC");
PORTA = 0b01100110;
speed = 0b11010000 ;
if(direction == 0){
CCPR1L = 0b11010000;
}else{
CCPR2L = 0b11010000;
}
break;
case 5:
currPos = 1;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
}
writeUART("FAN POSITION: 1");
PORTDbits.RD1 = 1;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 0;
prevPos = currPos;
break;
case 6:
prevPos = currPos;
currPos = 2;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
for(int i = currPos+1; i > prevPos; i--)
stepF();
}
writeUART("FAN POSITION: 2");
PORTDbits.RD1 = 0;
PORTDbits.RD0 = 1;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 0;
prevPos = currPos;
break;
case 7:
prevPos = currPos;
currPos = 3;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
for(int i = currPos+1; i > prevPos; i--)
stepF();
}
writeUART("FAN POSITION: 3");
PORTDbits.RD1 = 0;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 1;
PORTCbits.RC0 = 0;
prevPos = currPos;
break;
case 8:
prevPos = currPos;
currPos = 4;
if(prevPos > currPos){
for(int i = prevPos+1; i > currPos; i--)
stepB();
}else{
for(int i = currPos+1; i > prevPos; i--)
stepF();
}
writeUART("FAN POSITION: 4");
PORTDbits.RD1 = 0;
PORTDbits.RD0 = 0;
PORTCbits.RC3 = 0;
PORTCbits.RC0 = 1;
prevPos = currPos;
break;
case 9:
direction = 1;
CCP1CON = 0b00000000;
CCP2CON = 0b00111100;
CCPR2L = speed;
writeUART("FAN DIRECTION: REVERSED");
break;
case 10:
direction = 0;
CCP1CON = 0b00111100;
CCP2CON = 0b00000000;
CCPR1L = speed;
writeUART("FAN DIRECTION: FORWARD");
break;
default:
break;
}
}
void stepF(void){
for(int i = 0; i < 1; i++){
PORTB = 0b0001;
__delay_ms(100); //Delay between transitions
PORTB = 0b0010;
__delay_ms(100); //Delay between transitions
PORTB = 0b0100;
__delay_ms(100); //Delay between transitions
PORTB = 0b1000;
__delay_ms(100); //Delay between transitions
}
}
void stepB(void){
for(int i = 0; i < 1; i++){
PORTB = 0b1000;
__delay_ms(100); //Delay between transitions
PORTB = 0b0100;
__delay_ms(100); //Delay between transitions
PORTB = 0b0010;
__delay_ms(100); //Delay between transitions
PORTB = 0b0001;
__delay_ms(100); //Delay between transitions
}
}
void defaultPos(void){
PORTB = 0b1000;
__delay_ms(100); //Delay between transitions
PORTB = 0b0100;
__delay_ms(100); //Delay between transitions
PORTB = 0b0010;
__delay_ms(100); //Delay between transitions
PORTB = 0b0001;
__delay_ms(100); //Delay between transitions
}
void writeUART(const unsigned char string[]){
for(unsigned char j = 0; j < strlen(string); j++){
TXREG1 = string[j];
__delay_us(1000);
}
}
void setup(void){
isSetup = 1;
//PORTC
PORTC = 0x00;
LATC = 0x00;
TRISC = 0xC0; //Set RC6 & RC7 as inputs for EUSART
TRISCbits.RC6 = 0;
TRISCbits.RC7 = 1;
ANSELC = 0x00;
//PORTD
PORTD = 0x00;
LATD = 0x00;
TRISD = 0x00;
ANSELD = 0x00;
//PORTE
PORTE = 0x00;
LATE = 0x00;
TRISEbits.RE0 = 1;
TRISEbits.RE1 = 1;
ANSELE = 0x00;
//PORTB
PORTB = 0x00;
LATB = 0x00;
TRISB = 0x00;
ANSELB = 0x00;
PORTA = 0x00;
LATA = 0x00;
TRISA = 0x00;
ANSELA = 0x00;
//Oscillator
OSCCON = 0b01011100; //4 MHz oscillator
//EUSART
TXSTA1bits.BRGH = 1; //Highspeed baudrate
BAUDCON1bits.BRG16 = 0;
SPBRG1 = 12; //Baudrate of 19230 (FOSC = 4 MHz, BRGH = 1, BRG16 = 0)
TXSTA1bits.SYNC = 0; //Asynchronous
RCSTA1bits.SPEN = 1; //Enable rx & tx pins as serial pins
RCSTA1bits.CREN = 1; //Enable continuous reception, enable receiver
TXSTA1bits.TXEN = 1; //Enable transmitter
TXREG1 = 0x00;
RCREG1 = 0x00;
//Interrupts
RCONbits.IPEN = 1; //Enable priorities
INTCONbits.GIE_GIEH = 1; //Enable high priority interrupts
INTCONbits.PEIE_GIEL = 1; //Enable low priority interrupts
PIE1bits.TMR2IE = 1;
IPR1bits.TMR2IP = 0;
PIE1bits.RC1IE = 1; //Enable RX interrupt
PIR1bits.RC1IF = 0; //Clear interrupt flag
IPR1bits.RC1IP = 1; //High priority for RX interrupts
//PWM
PR2 = 0b11111001 ;
T2CON = 0b00000100; //1 KHz pulse frequency on CCP1 pin
CCPR1L = 0x00;
CCPR2L = 0x00;
CCP1CON = 0b00111100;
CCP2CON = 0b00000000;
TMR2 = 0;
}
A few suggestions,
Oscilliscope?
test with a light bulb rather than a DC motor, less current and voltage drop
this smells like a hardware problem

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