MPLAB code not working - pic

I am working on pic microcontroller programming and went lately to lcd made 2 codes, one in mplab and the other in mikroc actually both worked in isis but when I tried it in real only mikroc code worked in the microcontroller I don't know actually what is the problem/why did that happen I don't guess this is a hardware or software code because it is same circuit and both codes worked in isis so If anyone wanted to view the code so here it is:
mikroC:
// Lcd pinout settings
sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;
// Pin direction
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
void main() {
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 3, "Hello!");
}
mplab:
main.c:
#include "config.h"
unsigned char CurvyObject[8] = { 0x01,0x02,0x04,0x08,0x10,0x11,0x1f,0x00 };
void main(){
TRISB = 0;
TRISC = 0;
send_command(0x38);
__delay_us(40);
send_command(0x01);
__delay_ms(1.75);
send_command(0x0C);
__delay_us(40);
moveto(0, 0, 0, 0, 0, 0, 0);
__delay_us(40);
write_character(70);
__delay_us(50);
write_character(97);
__delay_us(50);
write_character(100);
__delay_us(50);
write_character(121);
__delay_us(50);
while(1);
}
config.h:
/*
* File: config.h
* Author: Fady
*
* Created on August 14, 2014, 6:19 PM
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = ON
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
#define _XTAL_FREQ 4000000
unsigned char i;
//Library Declaration:
void send_command(int command);
void write_character(int character);
void enable_blink();
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0);
void send_command(int command){
PORTB = command;
PORTCbits.RC0 = 0;
enable_blink();
}
void write_character(int character){
PORTB = character;
PORTCbits.RC0 = 1;
enable_blink();
}
void enable_blink(){
PORTCbits.RC1 = 1;
__delay_ms(10);
PORTCbits.RC1 = 0;
__delay_ms(10);
}
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0){
PORTCbits.RC0 = 0;
PORTBbits.RB7 = 1;
PORTBbits.RB6 = b6;
PORTBbits.RB5 = b5;
PORTBbits.RB4 = b4;
PORTBbits.RB3 = b3;
PORTBbits.RB2 = b2;
PORTBbits.RB1 = b1;
PORTBbits.RB0 = b0;
enable_blink();
}

It is a good practice to make sure all peripherals and outputs are in a known state before starting your program.
As mentioned in the comment, you never made sure the LCD has the R/W pin correctly configured.
PORTCbits.RC2=0; // The Microcontroller will always write to the LCD
// Replace RC2 with your R/W pin
In addition to that I compared your LCD library to the one I made and you didn't configure the entry mode, try adding this:
send_command(6); //Cursor Move direction and Display Shift
Check out this generic datasheet to see if you configured the module as desired: http://www.lcd-module.com/eng/pdf/doma/dip162-de.pdf
On a side note, I see that the moveto(); function is used to position the cursor around, may I suggest my approach:
// L1=128,L2=192,L3=144,L4=208
send_command(L1+6); // Go to the 6th character of the first line

Related

How to code for DHT11, pH Sensor with i2c 16x2 LCD in Arduino Uno?

I'm working on my college project on which I have to measure the temperature show it on 16x2 LCD and also to switch the cooling device according to the temperature, also I have to use pH Sensor and have to display its value on 16x2. Currently I'm using the code below for Temperature and switching, but it is not working properly. It shows the temperature on the screen but also some garbage value, and also not switching properly.
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
dht DHT;
#define DHT11_PIN 7
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
const int ledPin = 6;
void setup()
{
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
pinMode(DigitalPin, OUTPUT);
}
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature);
float Hum=(DHT.humidity);
lcd.print("Temp C ");
lcd.setCursor(6,0);
lcd.print(temp);
lcd.setCursor(0,1);
lcd.println("Humid % ");
lcd.setCursor(6,1);
lcd.print(Hum);
delay(1000);
if (temp <= 29)
digitalWrite(8, LOW);
else if (temp>30)
digitalWrite(8, HIGH);
}
I will divide my answer into two parts:
Hardware:
Please describe your hardware configuration in order to narrow down your problem: How are you switching the relay? Is it optically coupled? Are you using a transistor? If so, what type? Does your relay have reverse diode protection? (Do not connect your relay directly to an Arduino pin)
Code
2.1. This for-loop in your setup() is incorrect:
for (int DigitalPin = 8; DigitalPin <= 8; DigitalPin++) {
pinMode(DigitalPin, OUTPUT);
}
You are indexing from 8 to 8, hence you don't need a for loop to declare a single pin as an output.
2.2. In general, you can assign your output pins to variables, or as a macro like you did for the DHT. Just be consistent. Additionally, your code needs general tidying up:
a . Start your code with all libraries calling
#include <Wire.h> // The Wire.h library is already called by the LiquidCrystal_I2C library, so you don't need to call it if you use the I2C one
#include <LCD.h> // You are using the LCD library...
#include <LiquidCrystal_I2C.h> // but also the I2C lcd library. You don't need both.
#include <dht.h> // Is this the latest version? Use Adafruit's
b. Try to group all your macros at the beginning. Declare pins or constants here:
// Using macros
#define DHT11_PIN 7
#define DigitalPin 8
#define DHT11_PIN 7
or using constants for pins. Choose one, not both:
// Group all your const int together, for the sake of clarity
const int DHT_PIN = 7;
const int DigitalPin = 8;
const int ledPin = 6;
c. I believe this constructor is not correct for the library you chose. Check the example from this library (assuming it is the same, which I believe it is)
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
try with this constructor, instead:
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
d. The setup() function needs some changes too:
void setup()
{
// You don't seem to be using the serial library. However, if you use it, it is
// preferable to start it first.
Serial.begin(9600);
// LCD configuration
lcd.begin(16, 2); // Always call the begin() method first
lcd.setBacklightPin(3,POSITIVE); // Then, use additional configuration methods.
lcd.setBacklight(HIGH);
lcd.clear();
// Once again, please check if you are using the right library.
// It might be the cause of the garbage you see.
// Initialize the type of pin
pinMode(DigitalPin, OUTPUT);
}
e. According to Adafruit's library (please see the examples), you are missing a Macro to declare the type of DHT sensor you are using:
#define DHTTYPE DHT11
f. In your main loop() you are reading values with some strange parenthesis:
int chk = DHT.read11(DHT11_PIN);
float temp=(DHT.temperature);
float Hum=(DHT.humidity);
which, according to the latest library, should be:
// The latest library does not have this prototype: int chk = DHT.read11(DHT11_PIN); What is it for in your dht lib?
float temp = DHT.readTemperature(); // instead of: float temp=(DHT.temperature); --> which should be DHT.temperature(); in any case
float hum = DHT.readHumidity(); // instead of: float Hum=(DHT.humidity);
TL;DR: Share your hardware in order to assist you. Update your libraries, clean-up your code, and use the right methods from the latest libraries.
Thankyou so much for your guidance, below is the working code for me:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 7 //digital pin for sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
pinMode(8, OUTPUT); //for configuring relay output
lcd.begin(16, 2);
dht.begin();
}
void loop() {
delay(1000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
lcd.print("NO CONNECTION");
return;
}
//Hum code
lcd.setCursor(0, 0);
lcd.print("Hum:");
lcd.print(h);
lcd.print(" % ");
//Temp code
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.print(t);
lcd.print(" C");
//To operate relay
{
if (t <= 26)
digitalWrite(8, LOW);
else if (t>27)
digitalWrite(8, HIGH);
}
}

Pulse width modulation (PWM) on Attiny 9

I am using an Attiny 9.
I connected a LED to PB1.
I wrote following code and upload it to the Attiny 9.
#define F_CPU 800000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/power.h>
#define D0 PB1
int main(void)
{
power_adc_disable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
TCCR0A |= (1<<WGM00)|(1<<WGM01)|(1<<COM0B1);
TCCR0B |= (1<<WGM03)|(1<<WGM02)|(1<<CS00);
DDRB |= 1<<D0;
PORTB |= 1<<D0;
_delay_ms(3000);
PORTB &= ~(1<<D0);
for (uint8_t i = 255; 247 < i; --i)
{
OCR0B = i;
_delay_ms(70);
}
for (uint8_t i= 247; -1 < i; --i)
{
OCR0B = i;
_delay_ms(4);
}
while(1){
sleep_enable();
sleep_cpu();
}
return 0;
}
I want the LED to be lighten and darken. But after I uploaded the code, the LED illuminates continuously. I also read a datasheet of Attiny 9. I followed how to use PWM. But it's not working.
What's wrong?
There are a number of issues with that code.
You're putting the processor in to power down mode, which according to the datasheet, disables the clocks and only asynchronous events can take place.
You're setting it to use fast PWM mode with OCR0A as your top, but you aren't setting OCR0A anywhere. You won't generate any compare matches if you your timer can't count high enough. It would appear from your loops that you are expecting the timer to count to 255.
You only change the duty cycle of the PWM before your while loop, so even if your PWM gets set up correctly, you won't ever be able to see your LED pulse like you want.
Here's a link giving an example of how to set up a PWM and change its duty cycles:
http://allaboutavr.com/index.php/2017/05/13/chapter-5-timers-and-pwm-pulse-width-modulation/
Ty something like this:
#include <avr/io.h>
#include <util/delay.h>
#define D0 PB1
int main(void)
{
DDRB |= 1<<D0;
TCCR0A |= (1<<WGM00)|(1<<COM0B1);
TCCR0B |= (1<<WGM02)|(1<<CS00);
while (1) {
for (uint8_t i = 0; i < 255; i++) {
OCR0B = i;
_delay_ms(4);
}
for (uint8_t i = 255; i >= 0; i--) {
OCR0B = i;
_delay_ms(4);
}
}
}

Atmel Studio- ATmega128 bootloader

I am trying to write a customized boot-loader for ATmega AVR's. I write a code, and it work perfectly in small AVR's like ATmega32A and ATmega8A. But when i want to use it in ATmega128A, it writes nothing in flash segment.
I'm sure Fuses are correct, ATmega103 mode is disabled, and the program starts at boot section, but it does nothing..
Before calling function "boot_program_page" I set PORTC and turn some LED's on, and after that I cleared PORTC and LED's goes off. so the code in executing completely too.
The function I am using is an example provided in avr/boot.h.
It should write some data ( 0x00 actually ) in page 0 of flash memory..
here is my code :
#define F_CPU 8000000UL
#include <inttypes.h>
#include <avr/io.h>
#include <avr/boot.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
void boot_program_page (uint32_t page, uint8_t *buf);
int main(void)
{
uint8_t data[SPM_PAGESIZE] = {0};
uint32_t page = 0;
DDRC = 255;
PORTC = 255;
page *= (uint32_t)SPM_PAGESIZE;
boot_program_page(page,data);
_delay_ms(1000);
PORTC = 0;
while (1)
{
}
}
void boot_program_page (uint32_t page, uint8_t *buf)
{
uint16_t i;
uint8_t sreg;
// Disable interrupts.
sreg = SREG;
cli();
eeprom_busy_wait ();
boot_page_erase (page);
boot_spm_busy_wait (); // Wait until the memory is erased.
for (i=0; i<SPM_PAGESIZE; i+=2)
{
// Set up little-endian word.
uint16_t w = *buf++;
w += (*buf++) << 8;
boot_page_fill (page + i, w);
}
boot_page_write (page); // Store buffer in flash page.
boot_spm_busy_wait(); // Wait until the memory is written.
// Reenable RWW-section again. We need this if we want to jump back
// to the application after bootloading.
boot_rww_enable ();
// Re-enable interrupts (if they were ever enabled).
SREG = sreg;
}

MPLAB X IDE ADC code not working

i am a beginner to microcontroller and I just got into ADC but whenever I try to do a conversion it never works, I am trying to display the result on an LCD but the problem is not the LCD's because I tried its code alone and it worked so the problem is definitely the ADC's registers, here is the full code::
main.c:
#include "config.h"
int result;
void main(){
TRISAbits.TRISA0 = 1; //Set Port A for input
ADCON0 = 0b01000000; //Configuring ADCON0 register
ADCON1 = 0b10000000; //Configuring ADCON1 register
ADCON0bits.ADON = 1; //Turn on ADON bit in ADCON0 register to turn on ADC module
__delay_us(50); //Delay for the capacitor to be charged
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE == 1);
result = ADRESH && ADRESL;
initLCD();
write_character(result);
while(1);
}
config.h:
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4000000
void send_command(int command);
void write_character(int character);
void enable_blink(void);
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0);
void initLCD(void);
void send_command(int command){
PORTCbits.RC0 = 0;
PORTB = command;
enable_blink();
}
void write_character(int character){
PORTCbits.RC0 = 1;
PORTB = character;
enable_blink();
}
void enable_blink(){
PORTCbits.RC1 = 1;
__delay_ms(10);
PORTCbits.RC1 = 0;
__delay_ms(10);
}
void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0){
PORTCbits.RC0 = 0;
PORTBbits.RB7 = 1;
PORTBbits.RB6 = b6;
PORTBbits.RB5 = b5;
PORTBbits.RB4 = b4;
PORTBbits.RB3 = b3;
PORTBbits.RB2 = b2;
PORTBbits.RB1 = b1;
PORTBbits.RB0 = b0;
enable_blink();
}
void initLCD(){
TRISB = 0;
TRISC = 0;
send_command(0x38);
__delay_us(40);
send_command(0x01);
__delay_ms(1.75);
send_command(0x0C);
__delay_us(40);
}
You may have a problem with your result line. Try the following:
result = ( ( ADRESH << 8 ) | ( ADRESL ) )
The double & operator ( && ) is the logical AND. AND will return true ( 1 ) as long as ADRESH and ADRESL are both greater than 1. You want the bitwise OR ( | ) operator to combine two bytes into one int, with the ADRESH as the most-significant-byte and the ADRESL as the least-significant-byte.
Hopefully this works!

PIC USART code not working

I have covered lately adc and lcd in microcontroller and gone into USART and as usual my first code I make is not working and I need some help with discovering the problem, here's the code:
Transmitter code:
main.c:
#include "config.h"
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 1;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
while(TRMT == 0);
if(PORTDbits.RD0 == 1){
TXREG = 0xFF;
}else{
TXREG = 0;
}
}
}
Receiver code:
main.c:
#include "config.h"
char recieve;
void main(){
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
TRISDbits.TRISD0 = 0;
PORTDbits.RD0 = 0;
SPBRG = 25;
TXSTAbits.TX9 = 0;
TXSTAbits.SYNC = 0;
TXSTAbits.BRGH = 1;
TXSTAbits.TXEN = 1;
RCSTAbits.SPEN = 1;
RCSTAbits.RX9 = 0;
RCSTAbits.CREN = 0;
while(1){
RCREG = recieve;
if(recieve == 0xFF){
PORTDbits.RD0 = 1;
}else{
PORTDbits.RD0 = 0;
}
}
}
and for both transmitter and receiver projects config.h is a header file where I set the frequency of crystal oscillator and configuration bits so it is the same file/code for both projects
config.h:
/*
* File: config.h
* Author: Fady
*
* Created on August 25, 2014, 1:53 PM
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4000000
EDIT
I am trying to communicate 2 pic microcontrollers in which if I press the button on D0 in transmitter it sends a 0xFF data and then the receiver checks for if the received data == 0xFF if yes it turns on the LED on D0 in receiver but when I connect it on isis the TX pin of receiver keeps blinking high and low (red and blue) each half a second but when I press the button the high (red) signal keeps for a bit longer but keeps flashing with high and low and I suppose this is for start bit but the receiver doesn't turn on the led that's the error part I don't know what's wrong here
Its just a very stupid silly c programming mistake I wrote:
RCREG = recieve;
which will take the value of receive and assign it to RCREG instead it should be
receive = RCREG;
which will assign the value of RCREG to receive and worked after building...

Resources