Help...
Hi All, I realise this probably has been done to death a million or more times but I am struggling to find the information I require.
I am old school - started out many years ago 2002 with the Micro Chip PIC (16F636, 16F684, 16F877, & 16F84) NEC-78K Series, Mitsubishi M16C/62P, ATMEL (ATtiny26L, ATmega8)
But since then apart from a brief section in 2010 when I need to design and build a four channel data logger based on a 16F877 with RS232 comms and 4x16 LCD Screen.
Wife brought me the Arduino Mega 2560 a few years ago as a Christmas Present and last few days I have picked it up to have a play and get back into programming as I miss it...
But things have moved on so much that I do not recognise anything - looking at sample source code all the headers appear to be missing - do you still need to refer to the various library's if so where the hell are they located - what is this sketch pad how does this work?
Can someone please point me in the right direction as I feel like I have landed on an Alien Planet lol
So will the AT Mega8 code look at all remotely similar?
As I am a new member and unable to upload attachments I am placing some example code below from 2010 for the PIC 16F877 - this is how I used to do things - is this still current?
Delays appear to be in mills or similar wording from what I can find.
All I want is a basic setup file to get be started and back up to speed rest I can work out lol
/*==================================================================================
** COMPANY = Wolverhampton University - Third Year Project
** PROJECT = Temperature Data Recorder
** MODULE =
** VERSION = 0.0
** DATE = 28-07-2010
** LAST CHANGE =
** =================================================================================
** Description: Main Temperature Recording Programme
**
** Enviroment: Device: PIC 16F877 Version:
** Assembler: HI-TECH PICC Lite Version: Version 9.60
** C-Compiler: HI-TECH PICC Lite Version: Version 9.60
** Linker: HI-TECH PICC Lite Version: Version 9.60
** Programmer: PICkit(tm)1 Version: Flash Starter Kit, V1.60.0
** Firmware Version 2.0.2.
**
** =================================================================================
** By: James Mason
** =================================================================================
Changes:
** =================================================================================
*/
//#define RUN_TESTS
#include <htc.h>
#include "portability.h" // You MUST read portability.h for all macros X_()
#define PIC_HW
#include "hardware.h" // All hardware definitions
#include "lcd.h"
#include "delay.h"
#include "DS18B20.h"
#include <stdio.h>
#include <string.h>
#include "Serial.h"
#include "EEPROM.h"
#include "DS1307.h"
#include "Tests.h"
#include "User.h"
// Function Prototypes
//char test_buttons(void);
//void run_tests(void);
void do_readings(void);
void put_sleep(void);
//void set_time(void);
//void set_date(void);
//Global Variables
//eeprom int reading_address;
unsigned char shadowB=0;
unsigned char SerialFlag;
char uart_data = 0;
int user_num = 0;
unsigned char eeprom_data[10];
volatile long eeprom_address = 0;
long reading = 1;
volatile char int_flag;
char start_readings = 0;
char start_serial;
static bit button_pressed;
static bit sleep;
char serial_present;
/**** INTERRUPT ****/
void interrupt timer1(void)
{
if(RBIF)
{
RBIF = 0;
}
if(TMR1IF && TMR1IE)
{
TMR1IF = 0; // Clear interrupt flag
int_flag = 1; // Set flag to trigger routine in main
TMR1ON = 0;
NOP();
TMR1H = 0xFC;
TMR1L = 0x7C; // Set timer for a count of 60 (FC7C = 15 mins at 1Hz) FFC4 = 1min FFEC = 20 seconds
NOP();
TMR1ON = 1;
}
}
/**** MAIN PROGRAM ****/
void main(void)
{
int i = 0;
int button_timer = 0x1FFF;
int_flag = 0; // initialise interrupt flag
serial_present = 0; // initialise flags to 0
start_serial = 0;
button_pressed = 0;
sleep = 0;
restore_context(); // Restore reading address if power has failed whilst readings are being taken
LCD_POWER = 0; // Turn LCD on
init(); // initialise ports etc
i2c_init(); // initialise I2C
lcd_init(); // initialise LCD
RTC_StartOutput(); // Start RTC and output 1Hz square wave
DelayMs(200);
lcd_clear();
#if defined(RUN_TESTS)
run_eeprom_test();
run_tests(); // Does not return - uncomment to run tests
#endif
lcd_puts("TEMP LOGGER");
lcd_goto(0x40);
lcd_puts("v1.0.0");
DelayMs(2000);
lcd_goto(0x40);
lcd_puts("By James Mason");
DelayMs(2000);
lcd_clear();
lcd_goto(0x00);
lcd_puts("Press button");
lcd_goto(0x40);
lcd_puts(" to start");
if(start_readings)
{
lcd_clear();
lcd_puts("Continuing");
lcd_goto(0x40);
lcd_puts("Readings");
DelayMs(1000);
}
/********** MAIN LOOP ***************************/
/* Tests switches and runs according functions */
/* */
/************************************************/
while(1)
{
char button = test_buttons(); // Test for button press
button_timer--;
if(button_timer == 0) put_sleep(); // If time out without button press, then sleep
if(sleep) // On wake up from sleep
{
sleep = 0; // Turn sleep flag off
button_timer = 0x1FFF; // Reset button timer
RBIE = 0; // Turn button interrupt off, to enable buttons to be used
//LCD_POWER = 0; // Turn LCD on
//DelayMs(100);
//lcd_init();
//lcd_clear();
if(int_flag == 0 && TMR1IE == 1) // Check if wake up is from button press
{
LCD_POWER = 0; // Turn LCD on
DelayMs(100);
lcd_init();
lcd_clear();
read_eeprom_temp_lcd(eeprom_address - 16); // Read back last reading to check data has written correctly
lcd_goto(0x08); // and disply on LCD
read_eeprom_temp_lcd((eeprom_address - 16) + 2);
lcd_goto(0x40);
read_eeprom_temp_lcd((eeprom_address - 16) + 4);
lcd_goto(0x48);
read_eeprom_temp_lcd((eeprom_address - 16) + 6);
lcd_goto(0x10);
lcd_puts("Rd: ");
lcd_putdec(reading - 1);
lcd_goto(0x50);
read_time_eeprom_lcd((eeprom_address - 16) + 8);
lcd_goto(0x58);
read_date_eeprom_lcd((eeprom_address - 16) + 11);
}
}
if(button == BUTTON1)
{
button_timer = 0x1FFF;
if(LCD_POWER == 1)
{
LCD_POWER = 0;
DelayMs(100);
lcd_init();
lcd_clear();
read_eeprom_temp_lcd(eeprom_address - 16); // Read back last reading to check data has written correctly
lcd_goto(0x08); // and disply on LCD
read_eeprom_temp_lcd((eeprom_address - 16) + 2);
lcd_goto(0x40);
read_eeprom_temp_lcd((eeprom_address - 16) + 4);
lcd_goto(0x48);
read_eeprom_temp_lcd((eeprom_address - 16) + 6);
lcd_goto(0x10);
lcd_putdec(reading - 1);
}
int i = 5;
if(start_readings == 0)
{
start_readings = 1; // Start temp sensor readings
lcd_clear();
lcd_puts("Readings started");
}
TMR1IE = 1; // Enable Timer interrupt for taking readings
while(button == BUTTON1) // If button held down
{
button_pressed = 1;
button = test_buttons();
DelayMs(1000);
i--;
if(i == 0) // If held for >5 seconds
{
eeprom_address = 0; // reset reading address
reading = 1; // reset reading counter (for display on LCD)
lcd_clear();
lcd_puts("Readings stopped");
lcd_goto(0x40);
lcd_puts(" and zeroed");
start_readings = 0;
save_context();
TMR1IE = 0;
TMR1ON = 0; // Turn timer off to change value
NOP();
TMR1H = 0xFF; // Set for next reading in a few seconds
TMR1L = 0xFD;
TMR1ON = 1;
}
}
DelayMs(500); // for debug, remove later
lcd_clear();
}
if(button == BUTTON2)
{
button_timer = 0x1FFF;
i = 5;
while(button == BUTTON2)
{
button_pressed = 1;
button = test_buttons();
DelayMs(1000);
i--;
if(i == 0) // If held for 5 seconds, go to set time
{
lcd_clear();
lcd_goto(0x00);
lcd_puts("Set Time");
set_time();
}
}
}
if(button == BUTTON3)
{
button_timer = 0x1FFF;
i = 5;
while(button == BUTTON3)
{
button_pressed = 1;
button = test_buttons();
DelayMs(1000);
i--;
if(i == 0) // If held for 5 seconds, go to set date
{
lcd_clear();
lcd_goto(0x00);
lcd_puts("Set Date");
set_date();
}
}
}
if(button == BUTTON4)
{
button_timer = 0x1FFF;
i = 5;
while(button == BUTTON4)
{
button_pressed = 1;
button = test_buttons();
DelayMs(1000);
i--;
if(i == 0) // If held for 5 seconds, activate serial comms
{
start_serial = 1;
lcd_clear();
lcd_goto(0x00);
lcd_puts("Serial Active");
}
}
}
if(start_readings) do_readings(); // take readings if started
serial_changed(); // Check for serial cable plugged in
if(start_serial && serial_present) serial_commands(); // put last as can block interrupt
}
} // end of main
// Takes readings from sensors and writes to eeprom with time/date stamp
void do_readings()
{
char output[10] = {0,0,0,0,0,0,0,0,0,0};
char crc = 0;
char temp;
if(int_flag == 1)
{
int_flag = 0;
LED2 = 1;
/*
LCD_POWER = 0;
DelayMs(150);
lcd_init();
DelayMs(150);
lcd_clear();
lcd_goto(0x00);
lcd_puts("Take Reading");
lcd_goto(0x40);
lcd_putdec(reading);
DelayMs(1500);
*/
crc = write_eeprom_temp(eeprom_address, DQ1); // Write sensor 1 data to eeprom
if(crc)
{
write_ext_eeprom((eeprom_address + 14), 0x80);
}
else
{
write_ext_eeprom((eeprom_address + 14), 0x00);
}
crc = write_eeprom_temp(eeprom_address + 2, DQ2); // sensor 2
if(crc)
{
temp = read_ext_eeprom(eeprom_address + 14);
write_ext_eeprom((eeprom_address + 14), (temp | 0x08));
}
crc = write_eeprom_temp(eeprom_address + 4, DQ3); // sensor 3
if(crc)
{
write_ext_eeprom((eeprom_address + 15), 0x80);
}
else
{
write_ext_eeprom((eeprom_address + 15), 0x00);
}
crc = write_eeprom_temp(eeprom_address + 6, DQ4); // sensor 4
if(crc)
{
temp = read_ext_eeprom(eeprom_address + 15);
write_ext_eeprom((eeprom_address + 15), (temp | 0x08));
}
write_time_eeprom(eeprom_address + 8); // Write time to eeprom
write_date_eeprom(eeprom_address + 11); // Write date to eeprom
/*
// Display data written to eeprom
lcd_clear();
read_eeprom_temp_lcd(eeprom_address); // Read back to check data has written correctly
lcd_goto(0x08); // and display on LCD
read_eeprom_temp_lcd(eeprom_address + 2);
lcd_goto(0x40);
read_eeprom_temp_lcd(eeprom_address + 4);
lcd_goto(0x48);
read_eeprom_temp_lcd(eeprom_address + 6);
lcd_goto(0x14);
RTC_ReadTimeLCD();
lcd_goto(0x54);
RTC_ReadDateLCD();
*/
eeprom_address+=16; // increment reading address
reading++; // increment reading counter
save_context(); // Save reading address + counter in case of power failure
DelayMs(2000);
LED2 = 0;
if(reading >= 6720) // Reading number to stop at (6720 = 4 * 24 hours * 70 days - for readings every 15 min for 10 weeks)
{
lcd_clear();
lcd_goto(0x00);
lcd_puts("Readings");
lcd_goto(0x40);
lcd_puts("complete");
TMR1IE = 0;
while(test_buttons() == 0)
{
LED2 = 1;
DelayMs(500);
LED2 = 0;
DelayMs(1000);
}
}
}
}
// Sends PIC to sleep and turns off LCD for power saving
void put_sleep()
{
LED1 = 0;
LED2 = 0;
PORTD = 0b00000000; // Make sure all sensor pins are low, and LCD pins are low
RBIE = 1; // Turn on button interrupts for wake up
LCD_POWER = 1; // Turn off LCD
sleep = 1; // Set sleep flag
SLEEP(); // Sleep
}