Can not take Analog read in PIC 12F675 - c

In my code I want to read voltage from a battery and if the voltage greater than 3V then it off the LEDs.But the LED always on even when the voltage is 5v and when I add more LED in different GPIO and turn them 0/1 then the GPIO2 become off. I am using PIC12F675.
AN1 is my analog reader pin which is GPIO1.
GPI02 is my LED output pin.
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 4000000 // 4MHZ crystal
void main(void) {
unsigned int adcVal;
double voltage;
int i=0;
TRISIObits.TRISIO1 = 1;
TRISIObits.TRISIO2 = 0;
TRISIObits.TRISIO3 = 0;
ANSELbits.ADCS0 = 1;
ANSELbits.ADCS1 = 0;
ANSELbits.ADCS2 = 1; //FOSC/16
ANSELbits.ANS1 = 1; //channel 2
ADCON0bits.CHS0 = 1;
ADCON0bits.CHS1 = 0; //AN1
ADCON0bits.ADON = 1; //Turn it on
ADCON0bits.GO = 1;
ADCON0bits.ADFM = 1;
while (1) {
__delay_us(5);
ADCON0bits.ADON = 1;
GO_nDONE = 1;
while (GO_nDONE); //Wait for ADC to complete
adcVal = (((unsigned int) ADRESH << 8) + ADRESL);
ADCON0bits.ADON = 0;
voltage = ((double) (adcVal / 1023)*5.0);
if (voltage >= 3.0) {
GPIObits.GP2 = 0; //LED off
} else {
GPIObits.GP2 = 1; // LED On/
}
}
}

Your calculation adcVal/1023 is an integer calculation and for a range 0 ... 1023 for adcVal always 0 (or 1 for 1023).
Best thing is to avoid the float calculatin at all:
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 4000000 // 4MHZ crystal
void main(void) {
unsigned int adcVal;
double voltage;
int i=0;
TRISIObits.TRISIO1 = 1;
TRISIObits.TRISIO2 = 0;
TRISIObits.TRISIO3 = 0;
ANSELbits.ADCS0 = 1;
ANSELbits.ADCS1 = 0;
ANSELbits.ADCS2 = 1; //FOSC/16
ANSELbits.ANS1 = 1; //channel 2
ADCON0bits.CHS0 = 1;
ADCON0bits.CHS1 = 0; //AN1
ADCON0bits.ADON = 1; //Turn it on
ADCON0bits.GO = 1;
ADCON0bits.ADFM = 1;
while (1) {
__delay_us(5);
ADCON0bits.ADON = 1;
GO_nDONE = 1;
while (GO_nDONE); //Wait for ADC to complete
adcVal = (((unsigned int) ADRESH << 8) + ADRESL);
ADCON0bits.ADON = 0;
if (adcVal >= 614) { //value for 3.0V
GPIObits.GP2 = 0; //LED off
} else {
GPIObits.GP2 = 1; // LED On/
}
}
}

Related

why LCD does not display anything

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <stdint.h>
#include "config.h"
#include "Uart.h"
#define _XTAL_FREQ 20000000
#define RS RC0
#define EN RC1
#define D4 RC2
#define D5 RC3
#define D6 RC4
#define D7 RC5
/*
*
*/
void Lcd_Port(char a)
{
if(a & 1)
D4 = 1;
else
D4 = 0;
if(a & 2)
D5 = 1;
else
D5 = 0;
if(a & 4)
D6 = 1;
else
D6 = 0;
if(a & 8)
D7 = 1;
else
D7 = 0;
}
void Lcd_Cmd(char a)
{
RS = 0; // => RS = 0
Lcd_Port(a);
EN = 1; // => E = 1
__delay_ms(4);
EN = 0; // => E = 0
}
int Lcd_Clear()
{
Lcd_Cmd(0);
Lcd_Cmd(1);
}
void Lcd_Set_Cursor(char a, char b)
{
char temp,z,y;
if(a == 1)
{
temp = 0x80 + b - 1;
z = temp>>4;
y = temp & 0x0F;
Lcd_Cmd(z);
Lcd_Cmd(y);
}
else if(a == 2)
{
temp = 0xC0 + b - 1;
z = temp>>4;
y = temp & 0x0F;
Lcd_Cmd(z);
Lcd_Cmd(y);
}
}
void Lcd_Init()
{
Lcd_Port(0x00); // clear latches before enabling TRIS bits
__delay_ms(20);
Lcd_Cmd(0x03);
__delay_ms(5);
Lcd_Cmd(0x03);
__delay_ms(11);
Lcd_Cmd(0x03);
/////////////////////////////////////////////////////
Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the LCD
Lcd_Cmd(0x02);
Lcd_Cmd(0x08);//Select Row 1
Lcd_Cmd(0x00);//Clear Row 1 Display
Lcd_Cmd(0x0C);//Select Row 2
Lcd_Cmd(0x00);//Clear Row 2 Display
Lcd_Cmd(0x06);
}
void Lcd_Write_Char(char a)
{
char temp,y;
temp = a&0x0F;
y = a&0xF0;
RS = 1; // => RS = 1
Lcd_Port(y>>4); //Data transfer
EN = 1;
__delay_us(40);
EN = 0;
Lcd_Port(temp);
EN = 1;
__delay_us(40);
EN = 0;
}
void Lcd_Write_String(char *a)
{
int i;
for(i=0;a[i]!='\0';i++)
Lcd_Write_Char(a[i]);
}
void Lcd_Shift_Right()
{
Lcd_Cmd(0x01);
Lcd_Cmd(0x0C);
}
void Lcd_Shift_Left()
{
Lcd_Cmd(0x01);
Lcd_Cmd(0x08);
}
int main(int argc, char** argv) {
OSCCONbits.IRCF = 0b1111; //set operating frequency to 31kHz (0b1111) for 16MHz
UART_init();
Lcd_Init();
// ->Setare Pini
//Pini motor DC
ANSELAbits.ANSA0 = 0; //set to digital pin
ANSELAbits.ANSA1 = 0; //set to digital pin
TRISAbits.TRISA0 = 0; //set as output
TRISAbits.TRISA1 = 0; //set as output
PORTAbits.RA0 = 0;
PORTAbits.RA1 = 0;
//Butoane
ANSELAbits.ANSA2 = 0; //set to digital pin
ANSELAbits.ANSA4 = 0; //set to digital pin
ANSELBbits.ANSB4 = 0; //set to digital pin
TRISAbits.TRISA2 = 1; //set as input
TRISAbits.TRISA4 = 1; //set as input
TRISBbits.TRISB4 = 1; //set as input
//LEDuri
//RC6 - RIGHT
//RC6 LEFT
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 0;
//Set as digital
ANSELCbits.ANSC6 = 0;
ANSELCbits.ANSC7 = 0;
//Folosire LCD
TRISCbits.TRISC0 = 0; //set as output
TRISCbits.TRISC1 = 0; //set as output
TRISCbits.TRISC2 = 0; //set as output
TRISCbits.TRISC3 = 0; //set as output
TRISCbits.TRISC4 = 0; //set as output
TRISCbits.TRISC5 = 0; //set as output
//pull up
//pull upurile
OPTION_REGbits.nWPUEN = 0;
WPUAbits.WPUA2 = 1;
WPUAbits.WPUA4 = 1;
WPUAbits.WPUA0 = 1;
WPUAbits.WPUA1 = 0;
WPUAbits.WPUA3 = 0;
WPUAbits.WPUA5 = 0;
WPUBbits.WPUB4 = 1;
char introducere;
UART_write_string("1. Move to right ");
UART_write('\r');
UART_write_string("2. Move to left");
UART_write('\r');
UART_write_string("4. Stop UART");
UART_write('\r');
PORTCbits.RC6 = 0;
PORTCbits.RC7 = 0;
while(1){
introducere = UART_read();
if (introducere == '1'){
PORTAbits.RA0 = 1;
PORTAbits.RA1 = 0;
PORTCbits.RC6 = 1;
PORTCbits.RC7 = 0;
Lcd_Clear();
Lcd_Set_Cursor(1,1); //Go to the first line
Lcd_Write_String("helo"); //Display String
}
if (introducere == '2' ){
PORTAbits.RA0 = 0;
PORTAbits.RA1 = 1;
PORTCbits.RC6 = 0;
PORTCbits.RC7 = 1;
}
if(PORTAbits.RA2 == 0){ //Right
PORTAbits.RA0 = 1;
PORTAbits.RA1 = 0;
PORTCbits.RC6 = 1;
PORTCbits.RC7 = 0;
}
if(PORTAbits.RA4 == 0){ //left
PORTAbits.RA0 = 0;
PORTAbits.RA1 = 1;
}
if(introducere =='4'){
CREN = 0; //disable receiver
UART_write('\r');
UART_write_string("disable UART!");
UART_write('\r');
}
if(PORTBbits.RB4 == 0){
CREN = 1; //enable receiver
UART_write('\r');
UART_write_string("enable UART!");
UART_write('\r');
}
}
return (EXIT_SUCCESS);
}
The purpose of the project is to create a Dc motor which when we press 1 to rotate to the right and display on the LCD "rotated right", and if we press 2 to rotate to the left and display on the LCD "rotate left".
Used: pic16f1828
LCD: LM016L
I connected: RS (LCD) -> RC0 (pic), EN -> RC1, D4 -> RC2, D5 -> RC3, D6 -> RC4, D7 -> RC5
The program works well, the only problem I have is the fact that when I press "1" I want HELO to appear on LCD (it doesn't matter the arrangement). Till now it does not appear on LCD, is not just about HELLo, nothing is displayed, I can only see that it starts, I don't understand where the problem is.
Your implementation for writing to the HD44780 using a 4-bit parallel interface is wrong.
This is a complete, builds with MPLABX v5.50 and XC8 v2.32, demo that does display "Hello" on line 1 of the LCD module:
/*
* File: main.c
* Author: dan1138
* Target: PIC16F1828
* Compiler: XC8 v2.32
* IDE: MPLABX v5.50
*
* Created on January 23, 2022, 11:27 AM
*
* PIC16F1828
* +-----------:_:-----------+
* VDD -> : 1 VDD VSS 20 : <- VSS
* RA5 <> : 2 OSC2 PGD 19 : <> RA0 ICD_PGD
* RA4 <> : 3 OSC1 PGC 18 : <> RA1 ICD_PGC/D5
* ICD_VPP RA3 -> : 4 VPP/MCLRn 17 : <> RA2
* LCD_D7 RC5 <> : 5 16 : <> RC0 LCD_RS
* LCD_D6 RC4 <> : 6 15 : <> RC1 LCD_EN
* LCD_D5 RC3 <> : 7 14 : <> RC2 LCD_D4
* RC6 <> : 8 13 : <> RB4
* RC7 <> : 9 12 : <> RB5
* RB7 <> : 10 11 : <> RB6
* +-------------------------+
* DIP-20
*
* Description:
*
* See: https://stackoverflow.com/questions/70800375/why-lcd-does-not-display-anything
*/
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
/*
* Tell compiler what we intend to set the system oscillator frequency as.
*/
#define _XTAL_FREQ (16000000UL)
#include <xc.h>
#include <stdio.h>
/* Define the LCD interface and character size */
#define LCD_FORMAT (FOUR_BIT & LINES_5X7)
/* Define the LCD port pins */
#define LCD_DATA_BITS_MASK 0x3C
#define LCD_PORT_OUT LATC
#define LCD_PORT_DIR TRISC
#define LCD_RS_PIN LATCbits.LATC0
#define LCD_EN_PIN LATCbits.LATC1
#define LCD_D4_PIN LATCbits.LATC2
#define LCD_D5_PIN LATCbits.LATC3
#define LCD_D6_PIN LATCbits.LATC4
#define LCD_D7_PIN LATCbits.LATC5
#define LCD_RS_PIN_DIR TRISCbits.TRISC0
#define LCD_EN_PIN_DIR TRISCbits.TRISC1
#define LCD_D4_DIR TRISCbits.TRISC2
#define LCD_D5_DIR TRISCbits.TRISC3
#define LCD_D6_DIR TRISCbits.TRISC4
#define LCD_D7_DIR TRISCbits.TRISC5
/* Clear display command */
#define CLEAR_DISPLAY 0b00000001
/* Return home command */
#define RETURN_HOME 0b00000010
/* Display ON/OFF Control defines */
#define DON 0b00001111 /* Display on */
#define DOFF 0b00001011 /* Display off */
#define CURSOR_ON 0b00001111 /* Cursor on */
#define CURSOR_OFF 0b00001101 /* Cursor off */
#define BLINK_ON 0b00001111 /* Cursor Blink */
#define BLINK_OFF 0b00001110 /* Cursor No Blink */
/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT 0b00010011 /* Cursor shifts to the left */
#define SHIFT_CUR_RIGHT 0b00010111 /* Cursor shifts to the right */
#define SHIFT_DISP_LEFT 0b00011011 /* Display shifts to the left */
#define SHIFT_DISP_RIGHT 0b00011111 /* Display shifts to the right */
/* Function Set defines */
#define FOUR_BIT 0b00101111 /* 4-bit Interface */
#define EIGHT_BIT 0b00111111 /* 8-bit Interface */
#define LINE_5X7 0b00110011 /* 5x7 characters, single line */
#define LINE_5X10 0b00110111 /* 5x10 characters */
#define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */
/* Start address of each line */
#define LINE_ONE 0x00
#define LINE_TWO 0x40
static void LCD_E_Pulse(void)
{
LCD_EN_PIN = 1;
__delay_us(4);
LCD_EN_PIN = 0;
__delay_us(4);
}
static void LCD_DelayPOR(void)
{
__delay_ms(15);
}
static void LCD_Delay(void)
{
__delay_ms(5);
}
static void LCD_PutByte(unsigned char LCD_Data)
{
LCD_PORT_DIR &= ~LCD_DATA_BITS_MASK; /* make LCD data bits outputs */
/* send first(high) nibble */
LCD_PORT_OUT &= ~LCD_DATA_BITS_MASK;
if(LCD_Data & 0x10) LCD_D4_PIN = 1;
if(LCD_Data & 0x20) LCD_D5_PIN = 1;
if(LCD_Data & 0x40) LCD_D6_PIN = 1;
if(LCD_Data & 0x80) LCD_D7_PIN = 1;
LCD_E_Pulse();
/* send second(low) nibble */
LCD_PORT_OUT &= ~LCD_DATA_BITS_MASK;
if(LCD_Data & 0x01) LCD_D4_PIN = 1;
if(LCD_Data & 0x02) LCD_D5_PIN = 1;
if(LCD_Data & 0x04) LCD_D6_PIN = 1;
if(LCD_Data & 0x08) LCD_D7_PIN = 1;
LCD_E_Pulse();
LCD_PORT_DIR |= LCD_DATA_BITS_MASK; /* make LCD data bits inputs */
}
void LCD_SetPosition(unsigned char data)
{
LCD_RS_PIN = 0;
LCD_PutByte((unsigned char)(data | 0x80));
__delay_us(40);
}
void LCD_WriteCmd(unsigned char data)
{
LCD_RS_PIN = 0;
LCD_PutByte(data);
__delay_ms(4);
}
void LCD_WriteData(unsigned char data)
{
LCD_RS_PIN = 1;
LCD_PutByte(data);
LCD_RS_PIN = 0;
__delay_us(40);
}
void LCD_Init(void)
{
unsigned char LCD_Data;
LCD_PORT_DIR &= ~LCD_DATA_BITS_MASK; /* make LCD data bits outputs */
LCD_EN_PIN_DIR = 0; /* make LCD Enable strobe an output */
LCD_RS_PIN_DIR = 0; /* make LCD Register select an output */
LCD_EN_PIN = 0; /* set LCD Enable strobe to not active */
LCD_RS_PIN = 0; /* set LCD Register select to command group */
LCD_PORT_OUT &= ~LCD_DATA_BITS_MASK; /* set LCD data bits to zero */
LCD_DelayPOR(); /* wait for LCD power on to complete */
/* Force LCD to 8-bit mode */
LCD_PORT_OUT &= ~LCD_DATA_BITS_MASK; /* set LCD data bits to zero */
LCD_D4_PIN = 1;
LCD_D5_PIN = 1;
LCD_E_Pulse();
LCD_Delay();
LCD_E_Pulse();
LCD_Delay();
LCD_E_Pulse();
LCD_Delay();
/* Set LCD to 4-bit mode */
LCD_PORT_OUT &= ~LCD_DATA_BITS_MASK; /* set LCD data bits to zero */
LCD_D5_PIN = 1;
LCD_E_Pulse();
LCD_Delay();
/* Initialize LCD mode */
LCD_WriteCmd(LCD_FORMAT);
/* Turn on display, Setup cursor and blinking */
LCD_WriteCmd(DOFF & CURSOR_OFF & BLINK_OFF);
LCD_WriteCmd(DON & CURSOR_OFF & BLINK_OFF);
LCD_WriteCmd(CLEAR_DISPLAY);
LCD_WriteCmd(SHIFT_CUR_LEFT);
/* Set first position on line one, left most character */
LCD_SetPosition(LINE_ONE);
}
/*
* Hook for printf
*/
void putch(char txData)
{
LCD_WriteData(txData);
}
/*
* Main application
*/
void main(void)
{
OSCCON = 0x7A; //set operating frequency to 16MHz
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
//pull up
OPTION_REGbits.nWPUEN = 0;
WPUAbits.WPUA2 = 1;
WPUAbits.WPUA4 = 1;
WPUAbits.WPUA0 = 1;
WPUAbits.WPUA1 = 0;
WPUAbits.WPUA3 = 0;
WPUAbits.WPUA5 = 0;
LCD_Init();
__delay_ms(100);
LCD_SetPosition(LINE_ONE);
printf("Hello");
/*
* Application loop
*/
for(;;)
{
}
}

PIC Programming Counter and Touch Sensor Issue

I am new to this PIC programming and currently using C to program because ASM would take a bit time to learn. However, it seems like i am running into the most basic issue of all. The counter i setup with TMR0 is not working correctly and touch sensor does not seem to does anything which it supposed to reset the counter. Can anyone tell me where I did wrong? Been stuck on this for quite a bit. Thanks!
for PIC10F320
[Update]
Hi All, the thanks to the error caught by Kozmotronik. Recalculation was done + reassign bits as global interrupt seems to overflow for some reason. Also option_reg was taken out since T0CS was a 8 bit timer by default. simulation done in proteus for 10secs and timer looks fine now!
The only thing left is the reset from the touch sensor i am still figuring out. Attached a photo from my circuit and temporality replaced the touch sensor with normal button as there is no model for touch sensor. Please take a look if possible.Proteus circuit
// CONFIG
#pragma config FOSC = INTOSC // Oscillator Selection
#pragma config BOREN = OFF // Brown-out Reset
#pragma config WDTE = OFF // Watchdog Timer
#pragma config PWRTE = OFF // Power-up Timer
#pragma config MCLRE = OFF // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF // Code Protection
#pragma config LVP = OFF // Low-Voltage Programming
#pragma config LPBOR = OFF // Brown-out Reset Selection bits
#pragma config BORV = LO // Brown-out Reset Voltage Selection
#pragma config WRT = OFF // Flash Memory Self-Write Protection
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <pic10f320.h>
#define _XTAL_FREQ 8000000
#define SWITCH PORTAbits.RA2
#define PWMPin PORTAbits.RA1
#define LEDPin PORTAbits.RA0
unsigned long cnt;
/*
Main application
*/
void setup(void)
{
/**
LATx registers
*/
LATA = 0x00;
/**
TRISx registers
*/
TRISA = 0x04;
/**
ANSELx registers
*/
ANSELA = 0x00;
//Clear out the duty cycle registers
PR2 = 255;
T2CONbits.T2CKPS = 0b00;
T2CONbits.TMR2ON = 0x01;
/*
* My Calculations:
* At 8MHz, Tosc is 125ns, Tcyc=4*Tosc=500ns.
* With PR2=52decimal, prescaler=1, the PWM period is Tcyc(PR2+1) = 500ns*53 = 26.5us.
* For 100% duty cycle,
* PulseWidth = PWMxDCH/L (10bits) * Tosc * 1(prescaler) giving
* 26.5us/125ns=PWMxDCH/L = 212decimal
* PWMxDCH = d4h
* PWMxDCL = 00h
*
* For 75% duty cycle, the duty cycle registers must count off 26.5us*0.75=19.9us.
* PulseWidth = PWMxDCH/L (10bits) * Tosc * 1(prescaler) giving
* 19.9us/125ns=PWMxDCH/L = 159decimal
* PWMxDCH = 9fh
* PWMxDCL = 00h
*/
//Setting timer-0 for the generation of the delay
TMR0 = 0xFF; //Set TMR0 to 0
// OPTION_REG = 0x07;
//INTCON = 0xA0;
> INTCONbits.TMR0IF = 0; //Clear the TMR0 interrupt flag
> INTCONbits.TMR0IE = 1;
OPTION_REGbits.T0CS = 0; //Set TMR0 Clock source to FOS
OPTION_REGbits.PSA = 1; //Assign No-Prescaler to TMR0
/*
* My formulas is:
Fosc/4 = 8MHZ/4 = 2MHZ
period for a tick= 1 / 2MHZ = 0.0000005s
time for 8 bit count = 0.0000005 * 256 = 0.000128s
*
* Overflows every 128us
* for one second delay loop cnt = 1000000*(1/128) = 8
* LC = 7812.5*60 = 480 for 1 minute
* LC = 468750*60 = 28800 for 1 hour
* LC = 28125000*12 = 345600 for 12 hours
*/
}
void main(void)
{
setup();
cnt = 0;
//OSCCON = 2;
while(1){
PWMPin = 1;
LEDPin = 1;
if(SWITCH==1){
cnt++;
}
if(INTCONbits.TMR0IF){
cnt++; //Reset the timer
//100% Duty Cycle on RA1
> TMR0 = 0 ; /// reset TMR0 value
INTCONbits.TMR0IF=0; //Reset Timer Interrupt Flag
}
//If 10sec minute limit is reached (change this number to change the delay)
if(cnt>78125){
//75% Duty Cycle on RA1
// PWM1POL active_hi; PWM1OE enabled; PWM1EN enabled;
PWM2CON = 0xC0;
PWM1CONbits.PWM1OE = 0x01; //PWM1 Turn on
PWM1CONbits.PWM1EN = 0x01; //PWM1 Enable Output
PWM2DCH = 0x00;
PWM2DCL = 0x00;
PWM1DCH = 0x00;
PWM1DCL = 0x00;
PWM2DCH = 0xBF;
PWM2DCL = 0x00;
//LED breathing effect
for(int i=0;i<212;i++){
PWM1DCH = i;
__delay_ms(25);
}
for(int i=212;i>0;i--){
PWM1DCH = i;
__delay_ms(25);
}
}
}
}
Your timer overflow formula seems to be wrong.
Overflows every 128ms
is not true, I think you confused here. Time for 8 bit count is 0.000128s which is 128 microseconds for each timer overflow. So your loop count must be:
for one second delay loop cnt = 1000000*(1/128) = 7812,5
You must re-calculate the rest based on this corrected value. For the touch input you must ensure that you receive proper touch signals because we cannot diagnose it without seeing.

Why is my Timer2 interrupt not working? Am I missing any register configs?

I am working on a project with the pic10f322 microcontroller. I've made a very basic communication protocol - there is a start pulse (10 ms) followed by a number of 5ms pulses (2 pulses - turns on a red light, 3 turns on yellow and 4 - green). So the following code is trying to read the communication protocol and turn on the respective light. I'm using TMR0 to measure the length of the pulse and count it. I have a bicolour LED (Red and Green) so I need to alternate the two to create the yellow. I was hoping to use TMR2 as an interrupt to allow me to pulse the yellow light separately from the rest of the code, so that it doesn't get in the way of my main function detecting start pulses.
I have no idea why it isn't working. I've checked the registers (although please do double check incase I'm blind to something). The code compiles.
I turned the light on at various stages of the code to check it, and the light turns on in every case statement, include the last one where I set the LedColour enum variable to the respective colour. When I try to turn the light on in the interrupt function, it never turns on.
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <pic.h>
#include <stdbool.h>
#include <pic10f322.h>
// crystal oscilator
define _XTAL_FREQ 1000000
// CONFIG
#pragma config FOSC = INTOSC // Oscillator Selection bits
#pragma config BOREN = OFF // Brown-out Reset disabled
#pragma config WDTE = OFF // WDT disabled
#pragma config PWRTE = OFF // PWRT disabled
#pragma config MCLRE = OFF // MCLR pin function
#pragma config CP = OFF // Code Protection disabled
#pragma config LVP = ON // Low-voltage programming enabled
#pragma config LPBOR = OFF // Brown-out Reset disabled
#pragma config BORV = LO // Brown-out Reset Voltage, low trip point
#pragma config WRT = OFF // Flash Memory Write protection off
void timer2_isr(void);
#pragma code high_vector=0x08;
void interrupt (void)
{
asm("GOTO timer2_isr");
}
#pragma code
#pragma interrupt timer2_isr
#define RED_LED 0x01
#define GREEN_LED 0x02
#define SetBit(bit) (PORTA |= bit )
#define ClearBit(bit) (PORTA &= ~bit)
#define TestBit(bit) ( PORTA&bit)
int clkval = 0;
int pulsecnt = 0;
enum {
Red,
Green,
Yellow,
Off,
} LedColor = Off;
void timer2_isr (void)
{
PORTA = 0b1101; //This turns a green light on if it enters this function
if (PIR1 == 0x02)
{
PIR1 = 0x00;
}
}
void main(int argc, char** argv)
{
OSCCON = 0x30; //1MHz Clk
TRISA = 0x0C;
ANSELA = 0x00;
PORTA = 0x0C;
OPTION_REG = 0x06;
T2CON = 0x04; //Timer2 Registers Prescaler= 1 - TMR2 PostScaler = 1 - PR2 = 254 - Freq = 980.39 Hz - Period = 0.001020 seconds
PIE1 = 0x02;
PIR1 = 0x00;
TMR0 = 0;
TMR2 = 0;
PR2 = 254;
INTCON = 0xC0;
__delay_ms(2000);
enum {
WaitForStart,
CountPulses,
SelectColor,
} State = WaitForStart;
while (1)
{
switch (State)
{
case WaitForStart: //wait for start pulse
if ( (PORTA & 0x04) != 0x04 )
{
TMR0 = 0;
while ((PORTA & 0x04) != 0x04)
{
clkval = TMR0;
}
if (18 < clkval < 22)
{
State = CountPulses;
pulsecnt = 0;
}
}
break;
case CountPulses: // found start pulse, now count pulses or reset
if ( (PORTA & 0x04) != 0x04 )
{
TMR0 = 0;
while ((PORTA & 0x04) != 0x04)
{
clkval = TMR0;
}
if (8 < clkval < 12)
{
pulsecnt++;
}
}
if ((PORTA & 0x04) == 0x04)
{
clkval = 0;
TMR0 = 0;
while ((PORTA & 0x04) == 0x04 && clkval < 45)
{
clkval = TMR0;
if ((44 < clkval) || (pulsecnt > 4)) //no pulses noticed in over 22ms comparison or if you have surpassed the max number of pulses you are supposed to reach
{
if (pulsecnt > 0)
{
State = SelectColor;
} //if there has been a long delay, and pulses have been detect (so pulsecnt is greater than 0) then move to next case
else
{
State = WaitForStart;
} // if long delay and no pulses have been detected, restart and check for start pulse again
}
}
}
break;
case SelectColor: // if pulses have been detected, this state will be visited after long delay ( >22ms)
if (pulsecnt == 2)
{
LedColor = Red;
//PORTA = 0b1110;
State = WaitForStart;
}
else if (pulsecnt == 3)
{
LedColor = Yellow;
State = WaitForStart;
}
else if (pulsecnt == 4)
{
LedColor = Green;
//PORTA = 0b1101;
State = WaitForStart;
}
else
{
LedColor = Off;
State = WaitForStart;
}
break;
default:
State = WaitForStart;
break;
}
}
}
I used "PORTA = 0b1101", which turns the green light on. as a test line to step through the code and make sure it's reach certain points. Right now it is at the beginning of the interrupt, so it should turn on and stay on righht after the first interrupt which would happen within approximately 2.5ms I think? Or relatively quickly anyway, but it never gets inside the interrupt function or the function before which uses assembly to tell it to goto this function.
In PIC16, you need to start the timer separately from the rest of the config, like that:
T2CONbits.TMR2ON = 1;
Also, check that you have enabled the interrupts:
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
I suggest using the above notation for initialization; what you have is hard to verify.
The PIC 10 has got only one interrupt vector and that is on address 0x04 (not 0x08 as you expected).
Try somethig like this:
void interrupt myISR(void)
{
........
}
This init function gets a TMR2 interrupting at high priority
on a PIC18F25K80
void pwm_mosfet_stepper_init() {
TMR2 = 0;
RCON |= (1<<7); // IPEN = 1;
PR2 = 100; // 100; // 100==#16uS 1.6ms
//
INTCON |= ( (1<<6) | (1<<7) ); // GIE/GIEH PEIE/GIEL i.e. both low and high enabled
// .......................... post4 ON pre4
//T2CON = 0x1D; // 2us tick ON 11 1 01
// .......................... post4 ON pre16
T2CON = 0x1F; // 16us tick ON 11 1 1X
IPR1 |= (1<<1); // TMR2IP = 1; HIGH PRIORITY
PIE1 |= (1<<1); // TMR2IE = 1; // interrupts are GO!
}

TIMER 32-bit on pic32 can't use interrupts

Hi I have a PIC32MX370F512L from microchip.
I want to use TIMER2 and TIMER3 as a 32-bit timer for interrupts.
I CAN use 16-bit Timer interrupt but CAN'T use 32-bit Timer interrupt, I will show you both codes:
16-bit timer:
void init16(){
T2CONbits.ON = 0; //Timer disabled
T2CON = 0; //Stop any 16-bit Timer2 operation
T2CONbits.T32 = 0; //16 bit mode on, tmr2
T2CONbits.TCKPS = 0b100; //prescaler
T2CONbits.TCS = 0; //Select internal peripheral clock
PR2 = 0x0000FFFF;
TMR2 = 0; //Clear contents of TMR2
T2CONbits.ON = 1; //Timer enable
//====INTERRUPT PART========
IEC0bits.T2IE = 0; //Disable interrupt
IPC2bits.T2IP = 1; //Priority 1
//IPC3bits.T3IS = 0; //Sub-priority 3
IFS0bits.T2IF = 0; //Interrupt flag putted at zero
//IFS0bits.T3IF = 0; //Interrupt flag putted at zero
IEC0bits.T2IE = 1; //Enable interrupt
}
32-bit timer:
void init32(){
T2CONbits.ON = 0; //Timer disabled
T2CON = 0; //Stop any 16/32-bit Timer2 operation
T3CON = 0; //Stop any 16-bit Timer3 operation
T2CONbits.T32 = 1; //32 bit mode on, tmr2 + tmr3
T2CONbits.TCKPS = 0b100; //Prescaler
T2CONbits.TCS = 0; //Select internal peripheral clock
PR2 = 0x000FFFFF;
//PR3 = 0;
TMR2 = 0; //Clear contents of TMR2 and TMR3
T2CONbits.ON = 1; //Timer enable
//====INTERRUPT PART========
IEC0bits.T3IE = 0; //Disable interrupt
IPC3bits.T3IP = 1; //Priority 1
//IPC3bits.T3IS = 0; //Sub-priority 0
IFS0bits.T3IF = 0; //Interrupt flag putted at zero
//IFS0bits.T3IF = 0; //Interrupt flag putted at zero
IEC0bits.T3IE = 1; //Enable interrupt
}
main :
#include <stdio.h>
#include <stdlib.h>
#include <p32xxxx.h>
#include <plib.h>
#include "timer32Interrupt.h"
#include <xc.h>
/* Disable JTAG to use RA0 */
#pragma config JTAGEN = OFF
#pragma config FWDTEN = OFF //Watchdog disabled
/* Device Config Bits in DEVCFG1: */
#pragma config FNOSC = FRCPLL //Fast RC Osc with PLL
#pragma config FSOSCEN = OFF //Secondary Oscillator disabled
#pragma config POSCMOD = XT //Primary Oscillator mode: Resonator, crystal or resonator
#pragma config OSCIOFNC = ON //CLKO Output Signal Active on the OSCO Pin
#pragma config FPBDIV = DIV_2 //Peripheral Clock Divisor: PBCLK is SYSCLK divided by 2
/* Device Config Bits in DEVCFG2: */
#pragma config FPLLIDIV = DIV_2 //PLL Input Divider
#pragma config FPLLMUL = MUL_20 //PLL Multiplier
#pragma config FPLLODIV = DIV_2 //System PLL Output Clock Divider: PLL Divide by 2
void main(){
TRISA = 0;
LATA = 0;
LATAbits.LATA0 = 1;
//LATAbits.LATA0 =~LATAbits.LATA0
INTEnableSystemMultiVectoredInt();
init32();
//init16();
while(1){
int i = 0;
if(TMR2 >= PR2 - 10 ){
//LATAbits.LATA0 =~LATAbits.LATA0;
//LATAbits.LATA2 =~LATAbits.LATA2;
LATAINV = 0b10;
}
}
}
void __ISR(_TIMER_2_VECTOR, ipl1)Timer32Handler(void){
LATAINV = 0b101;
IFS0bits.T3IF = 0; //Interrupt flag putted at zero
IFS0bits.T2IF = 0; //Interrupt flag putted at zero
}
The 16 bit implementation works and the 32 not, enabling interrupt on TIMER3 and not on TIMER2 is required in the data-sheet, section 14.3.4 of the timers.
Like said by #Sudhee for making the code work I should replace only this part:
_TIMER_2_VECTOR became _TIMER_3_VECTOR
void __ISR(_TIMER_3_VECTOR, ipl1)Timer32Handler(void){
LATAINV = 0b101;
IFS0bits.T3IF = 0; //Interrupt flag putted at zero
IFS0bits.T2IF = 0; //Interrupt flag putted at zero
}

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

Resources