Pic12f683 only has digital out on pin 2 (GP5) - microcontroller

I've been trying to get started with the PIC12f683 in order to get familiar with the PIC family of microcontrollers.
I've got it hooked up to a Pickit 3, and talking to my PC, that all seems to work fine.
I've been trying various LED flasher examples from the internet but no matter which example code I try, or what I do with the TRISO or GPIO registers, I only get output on pin 2, none of the other GPIO pins will do anything.
Electrically speaking... Pins 1, 4, 6, 7, and 8 are connected to the Pickit for ICSP. Pins 2, 3, 5, and 7 have LEDs with 220 ohm resistors going from the pin to ground, and pins 1 and 8 are connected to a lithium ion battery (~3.7 v).
This is the code I am using currently.
#define _XTAL_FREQ 8000000
#include <xc.h>
// BEGIN CONFIG
// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/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 = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#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 BOREN = OFF // Brown Out Detect (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
//END CONFIG
int main()
{
TRISIO =0b00000000; //RB0 as Output PIN
GPIO = 0b00111111;
while(1)
{
GP5=1;
GP4=1;
GP0=1;
GP2=1;
__delay_ms(1000);
GP5=0;
GP4=0;
GP0=0;
GP2=0;
__delay_ms(1000);
}
return 0;
}
I believe the config portion was generated by MPlab, the rest is copied/modified from this instructable:
https://www.instructables.com/id/How-to-Blink-an-Led-With-PIC16F886-/
but the results are the same no matter what example code I use. I'm guessing there's a register or configuration variable I've set wrong somewhere, but I can't figure it out.

You had to switch your Outputs to digital.
int main()
{
TRISIO =0b00000000; //RB0 as Output PIN
ANSEL = 0; // all outputs digital !!!!!
CMCON0 = 0b00000111; //turn Cin Pins as I/O
GPIO = 0b00111111;
while(1)
{
GP5=1;
GP4=1;
GP0=1;
GP2=1;
__delay_ms(1000);
GP5=0;
GP4=0;
GP0=0;
GP2=0;
__delay_ms(1000);
}
return 0;
}
The outputs GP0 ... GP4 are default analog ones.
And please remember GP3 share the pin with MCLR, so it is an input only pin.

Related

How can I write to the flash memory of a PIC24FJ?

I am using a PIC microcontroller, exactly the PIC24FJ128GL305 and I want to save important data in flash memory using MPLAB X IDE V6, one of the data I want to save is -733.5847 and I can't save it, in fact I can't even save integers.
Could anyone help me with a library to do what I want, please?
There is a whole lot you will need to learn before this example will make any sense.
This code does work but it is not even close to what you will actually need to create.
Perhaps you will learn enough from a working example to be able to ask a better question.
/*
* File: main.c
* Author: dan1138
*
* Description:
* Example for Run Time Self Programming (RTSP).
* This is very limited, useful as a test bench but not much more.
*
* Created on December 14, 2022, 9:07 PM
*/
/* Define the system oscillator frequency this code must configure */
#define FSYS (8000000ul)
#define FCY (FSYS/2ul)
// PIC24FJ128GL305 Configuration Bit Settings
// 'C' source line config statements
// FSEC
#pragma config BWRP = OFF // Boot Segment Write-Protect bit (Boot Segment may be written)
#pragma config BSS = DISABLED // Boot Segment Code-Protect Level bits (No Protection (other than BWRP))
#pragma config BSEN = OFF // Boot Segment Control bit (No Boot Segment)
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = DISABLED // General Segment Code-Protect Level bits (No Protection (other than GWRP))
#pragma config CWRP = OFF // Configuration Segment Write-Protect bit (Configuration Segment may be written)
#pragma config CSS = DISABLED // Configuration Segment Code-Protect Level bits (No Protection (other than CWRP))
#pragma config AIVTDIS = OFF // Alternate Interrupt Vector Table bit (Disabled AIVT)
// FBSLIM
#pragma config BSLIM = 0x1FFF // Boot Segment Flash Page Address Limit bits (Enter Hexadecimal value)
// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config PLLMODE = DISABLED // PLL Mode Selection (No PLL used; PLLEN bit is not available)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then switch to user-selected oscillator source)
// FOSC
#pragma config POSCMD = NONE // Primary Oscillator Mode Select bits (Primary Oscillator disabled)
#pragma config OSCIOFCN = ON // OSC2 Pin Function bit (OSC2 is general purpose digital I/O pin)
#pragma config SOSCSEL = ON // SOSC Power Selection Configuration bits (SOSC is used in crystal (SOSCI/SOSCO) mode)
#pragma config PLLSS = PLL_PRI // PLL Secondary Selection Configuration bit (PLL is fed by the Primary oscillator)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration bit (Allow multiple reconfigurations)
#pragma config FCKSM = CSECMD // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)
// FWDT
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config FWPSA = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bits (WDT and SWDTEN disabled)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config WDTWIN = WIN25 // Watchdog Timer Window Select bits (WDT Window is 25% of WDT period)
#pragma config WDTCMX = WDTCLK // WDT MUX Source Select bits (WDT clock source is determined by the WDTCLK Configuration bits)
#pragma config WDTCLK = LPRC // WDT Clock Source Select bits (WDT uses LPRC)
// FPOR
#pragma config BOREN = OFF // Brown Out Enable bit (Brown Out Disabled)
#pragma config LPCFG = OFF // Low power regulator control (No Retention Sleep)
#pragma config DNVPEN = ENABLE // Downside Voltage Protection Enable bit (Downside protection enabled using ZPBOR when BOR is inactive)
// FICD
#pragma config ICS = PGD1 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
// FDMTIVTL
#pragma config DMTIVTL = 0xFFFF // Deadman Timer Interval Low Word (Enter Hexadecimal value)
// FDMTIVTH
#pragma config DMTIVTH = 0xFFFF // Deadman Timer Interval High Word (Enter Hexadecimal value)
// FDMTCNTL
#pragma config DMTCNTL = 0xFFFF // Deadman Timer Instruction Count Low Word (Enter Hexadecimal value)
// FDMTCNTH
#pragma config DMTCNTH = 0xFFFF // Deadman Timer Instruction Count High Word (Enter Hexadecimal value)
// FMDT
#pragma config DMTDIS = OFF // Deadman Timer Enable Bit (Dead Man Timer is Disabled and can be enabled by software)
// FDEVOPT1
#pragma config ALTCMPI = DISABLE // Alternate Comparator Input Enable bit (C2INC and C3INC are on their standard pin locations )
#pragma config TMPRPIN = OFF // Tamper Pin Enable bit (TMPRN pin function is disabled)
#pragma config SOSCHP = ON // SOSC High Power Enable bit (valid only when SOSCSEL = 1 (Enable SOSC high power mode (default))
#pragma config ALTI2C1 = ALTI2C1_OFF // Alternate I2C pin Location (I2C1 Pin mapped to SDA1/SCL1 pins)
#pragma config SMB3EN = SMBUS3 // SM Bus Enable (SMBus 3.0 input levels)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <libpic30.h>
#define MEMORY_USER_CALIBRATION_LOC (_FLASH_PAGE * 24)
typedef struct
{
float fCal;
} stFlashStruct_t;
volatile const __prog__ __attribute__((space(prog), address(MEMORY_USER_CALIBRATION_LOC))) union
{
uint16_t words[_FLASH_PAGE]; /* reserve the entire erase page. Note only the low 16-bits of the instruction word can be accessed with this method. */
struct {
stFlashStruct_t stFlashStruct; /* calibration structure */
};
} CalSpace =
{
.stFlashStruct.fCal = 1.0,
};
int main(void)
{
volatile stFlashStruct_t ReadBack;
/*
* application initialization
*/
ReadBack.fCal = CalSpace.stFlashStruct.fCal;
__builtin_software_breakpoint(); /* breakpoint here to inspect the ReadBack structure with the debugger */
Nop();
Nop();
/* Erase 1024 instruction words starting at address MEMORY_USER_CALIBRATION_LOC */
NVMCON = 0x4003;
NVMADR = __builtin_tbloffset(&CalSpace);
NVMADRU = __builtin_tblpage(&CalSpace);
__builtin_disi(5); // Disable interrupts for NVM unlock
__builtin_write_NVM(); // Start write cycle
while(NVMCONbits.WR == 1);
ReadBack.fCal = CalSpace.stFlashStruct.fCal;
__builtin_software_breakpoint(); /* breakpoint here to inspect the ReadBack structure with the debugger */
Nop();
Nop();
/* Update data in structure to be written */
ReadBack.fCal = -733.5847;
/* Write 2 instruction words starting at address MEMORY_USER_CALIBRATION_LOC */
NVMCON = 0x4001; // Set WREN and word program mode
TBLPAG = 0xFA; // write latch upper address
NVMADR = __builtin_tbloffset(&CalSpace.stFlashStruct);
NVMADRU = __builtin_tblpage(&CalSpace);
__builtin_tblwtl(0,*((uint16_t *)(&ReadBack)+0)); // load low 16-bits of first instruction word
__builtin_tblwth(0,0x00); // make high 8-bits of first instruction word zero
__builtin_tblwtl(2,*((uint16_t *)(&ReadBack)+1)); // load low 16-bits of second instruction word
__builtin_tblwth(2,0x00); // make high 8-bits of second instruction word zero
__builtin_disi(5); // Disable interrupts for NVM unlock sequence
__builtin_write_NVM(); // initiate write
while(NVMCONbits.WR == 1);
ReadBack.fCal = CalSpace.stFlashStruct.fCal;
__builtin_software_breakpoint(); /* breakpoint here to inspect the ReadBack structure with the debugger */
Nop();
Nop();
/*
* Application process loop
*/
for(;;)
{
Nop();
Nop();
Nop();
__delay_ms(100);
}
}

Arduino communication with TMC2209

I'm trying to communicate with TMC2209 (stepper drive) with an Arduino nano Every.
I connected pin RX on pin D2 and TX on pin D3.
I placed a 1K resistor between TX and RX.
It seems I can write parameters (even I'm testing this deeply, I'm not so sure now..) but I'm not able
to read nothing from driver.
Picture added 15/04/2021 related to datasheet of TMC2209
In my test, I tried with only one driver with address 0, means MS1_AD0 and MS2_AD1 connected to GND.
driver1.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
I tried reading IFCNT and DRV_STATUS, always ZERO.
The initialization is done in this way:
#include <Wire.h>
#include <AG_AS5600.h>
#include <TMCStepper.h>
#include <AccelStepper.h>
#define ENCODER false
#define FEEDBACK_I2C true
#define GEARBOX 139 //51
#define MICROSTEPS 16
#define MIN_SPEED 20
#define MAX_SPEED 3000
double offsetHome = 77.0;
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SerialPort SerialUSB
#define SYS_VOL 3.3
#else
#define SerialPort Serial
#define SYS_VOL 5
#endif
#define SW_RX 2 // TMC2208/TMC2224 SoftwareSerial receive pin
#define SW_TX 3 // TMC2208/TMC2224 SoftwareSerial transmit pin
#define EN_PIN 7 // Enable
#define DIR_PIN 8 // Direction
#define STEP_PIN 9 // Step
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS1 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // Match to your driver
// SilentStepStick series use 0.11
// UltiMachine Einsy and Archim2 boards use 0.2
// Panucatt BSD2660 uses 0.1
// Watterott TMC5160 uses 0.075
AG_AMS_5600 ams5600;
TMC2209Stepper driver1(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS1);
AccelStepper stepper = AccelStepper(stepper.DRIVER, STEP_PIN, DIR_PIN);
String cmd = {};
void setup()
{
SerialPort.begin(115200);
Wire.begin();
Wire.setTimeout(10);
SerialPort.println(">>>>>>>>>> Program started <<<<<<<<<<");
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); // Enable driver in hardware
// Enable one according to your setup
SPI.begin(); // SPI drivers
driver1.beginSerial(115200); // SW UART drivers
driver1.begin(); // SPI: Init CS pins and possible SW SPI pins
// UART: Init SW UART (if selected) with default 115200 baudrate
driver1.toff(5); // Enables driver in software
driver1.rms_current(2000); // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
driver1.microsteps(MICROSTEPS);
driver1.pwm_autoscale(true); // Needed for stealthChop
stepper.setMaxSpeed(degToSteps(500.0)); // steps/s
stepper.setAcceleration(degToSteps(1.0)); // steps/s^2
stepper.setEnablePin(EN_PIN);
stepper.setPinsInverted(false, false, true);
stepper.disableOutputs();
}
void loop()
{
unsigned long t0 = micros();
switch (step) {
case 0:
cmd = SerialPort.readString();
if (cmd.charAt(0)=='a' && cmd.charAt(1)=='=') {
acc = degToSteps(cmd.substring(2).toDouble());
SerialPort.print("Acceleration=");
SerialPort.println(acc);
cmd = "";
}
else if (cmd.charAt(0)=='d' && cmd.charAt(1)=='=') {
dec = cmd.substring(2).toInt();
SerialPort.print("Deceleration=");
SerialPort.println(dec);
cmd = "";
}
else if (cmd.charAt(0)=='c' && cmd.charAt(1)=='?') {
driver1.microsteps(MICROSTEPS);
driver2.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Counter2=");
SerialPort.print(driver2.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
cmd = "";
}
}
The hardware should be ok because the jumper for UART is configured as default on pin4.
Can someone help me to understand why?
Thanks
Andrea
I found!
The issue was the handling of "software" serial port.
Unfortunately I was confused because many examples of "TMCstepper.h" library are shown using two pins as TX and RX, so I was convinced internally pins were handled to send and receive.. but it's not.
So solution I found is:
#include <SoftwareSerial.h>
SoftwareSerial SerialDriver1(SW_RX, SW_TX);
TMC2209Stepper driver1(&SerialDriver1, R_SENSE, DRIVER_ADDRESS1);
void setup()
{
SerialDriver1.begin(57600);
SerialDriver1.listen();
...
and was really important to comment:
//driver1.beginSerial(115200); // SW UART drivers
this is important I guess because the port was already opened in the setup by me.
and it's necessary to specify the software port where to listen, not done by TMC library.
Last hint, with Arduino Nano Every I found out that Software Serial seems to work with max baudrate of 57600, 115200 was not working.

"SPCR was not declared" when trying to use an rgb matrix on an arduino uno wifi

I have tried several example programs to get an rgb matrix working on my arduino uno wifi. I feel like an idiot that I can't figure this out, and I've googled tons to no avail. The errors and code are below. The problem is that the rgbmatrix library is referencing a var that isn't defined. Am I missing another library I need to import? I am a total noob to arduino, but I've developed with netduino and particle photon, fwiw.
Thanks for any help. I'm trying to build my boy a rocket bookshelf with a cool display, but if I can't get past this, it's going to lose significant cool points :).
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp: In member function 'void cRGBMatrix::begin(int)':
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:5: error: 'SPCR' was not declared in this scope
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:5: note: suggested alternative: 'SCL'
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
SCL
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:16: error: 'SPE' was not declared in this scope
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:16: note: suggested alternative: 'PE'
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~
PE
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:25: error: 'MSTR' was not declared in this scope
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:25: note: suggested alternative: 'PSTR'
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
PSTR
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:35: error: 'SPR1' was not declared in this scope
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:35: note: suggested alternative: 'SPI0'
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
SPI0
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:45: error: 'SPR0' was not declared in this scope
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:48:45: note: suggested alternative: 'SPI0'
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Enable SPI HW, Master Mode, divide clock by 16 //SPI Bus setup
^~~~
SPI0
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp: In member function 'char cRGBMatrix::spiTransfer(char)':
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:78:5: error: 'SPDR' was not declared in this scope
SPDR = value; // Start the transmission
^~~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:78:5: note: suggested alternative: 'SDA'
SPDR = value; // Start the transmission
^~~~
SDA
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:79:14: error: 'SPSR' was not declared in this scope
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
^~~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:79:14: note: suggested alternative: 'SS'
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
^~~~
SS
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:79:25: error: 'SPIF' was not declared in this scope
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
^~~~
/Users/johnwest/Documents/Arduino/libraries/RGBMatrix/RGBMatrix.cpp:79:25: note: suggested alternative: 'SPI0'
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
^~~~
SPI0
exit status 1
Error compiling for board Arduino Uno WiFi Rev2.
^~~
#include <SPI.h>
#include <RGB.h>
#include <RGBMatrix.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}

Arduino builds fail with "Reset.h: No such file or directory"

When I'm trying to build what seems to be any code in the Arduino IDE, it fails with this error:
Arduino: 1.6.5 (Mac OS X), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/USBCore.cpp:19:19: fatal error: Reset.h: No such file or directory
#include "Reset.h"
^
compilation terminated.
Error compiling.
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
The code I'm trying to build is from https://www.arduino.cc/en/Tutorial/Fade:
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
The analogWrite() function uses PWM, so if
you want to change the pin you're using, be
sure to use another PWM capable pin. On most
Arduino, the PWM pins are identified with
a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Why is this happening? I don't think I've changed any of the Arduino libraries, but wondering if that's the case now. A find in /Applications/Arduino.app/ lists no files called Reset.h, however, I did find a ~/Library/Arduino15/packages/arduino/hardware/sam/1.6.4/cores/arduino/Reset.h.
Building with Arduino IDE 1.6.5, using a Mega 2560 and /dev/cu.usbmodem1421.
It turns out my Android installation was modified, and the modified USBCore.cpp wasn't really correct for this version (1.6.5) of Arduino. Reinstalling fixed the problem.

Pic18F2520 Reset

I am trying to control a BMP180 through I2C protocol. The problem is that the Pic resets all the time. I changed the Pic and it is the same.
The config code is the next:
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1H
#pragma config OSC = INTIO7 // Oscillator Selection bits (Internal oscillator block, CLKO function on RA6, port function on RA7)
#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)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)
// CONFIG5H
#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)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)
// CONFIG6H
#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)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)
and the main code:
#include <xc.h>
#include <stdint.h>
#define EAUSART_V4
#include <plib/usart.h>
#include "i2c.h"
#define OSCCON_Init 0b11111111
#define BMP180_W 0xEE
#define BMP180_R 0xEF
#define I2C_master_ACK 1
#define I2C_master_NOACK 0
#define I2C_WRITE_CMD 0
#define I2C_READ_CMD 1
#define I2C_START_CMD 0
#define I2C_REP_START_CMD 1
#define I2C_REQ_ACK 0
#define I2C_REQ_NOACK 0
#define SDA_TRIS TRISCbits.RC4
#define SCL_TRIS TRISCbits.RC3
char CaracterRx;
int nummer = 0;
uint8_t data;
uint16_t temperature;
uint8_t BMP180_present(void);
void i2c_master_ack(unsigned char);
uint16_t BMP180_Temperature_Lecture(void);
void main(void) {
char hello[] ={"hello there\r\n\0"};
SSPSTATbits.CKE = 1;
SDA_TRIS = 1;
SCL_TRIS = 1;
OSCCON = OSCCON_Init;
OpenUSART(USART_TX_INT_ON &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 51);
putsUSART("2");
for(int i=0; i<10 ; i++)
__delay_ms(50);
SSPADD=19;
OpenI2C(MASTER,SLEW_OFF);
putsUSART(&hello);
for(int i=0; i<10 ; i++)
__delay_ms(50);
while(1)
{
data = BMP180_present();
temperature = BMP180_Temperature_Lecture();
nummer = data;
putsUSART(nummer);
for(int i=0; i<50; i++)
__delay_ms(50);
}
}
uint8_t BMP180_present(void)
{//returns true if a bmp is detected.
//Detection achieved by looking at the device ID, which is fixed at 0x55.
uint8_t Id;
StartI2C();
WriteI2C(BMP180_W);
WriteI2C(0xD0);
RestartI2C();
WriteI2C(BMP180_R);
Id=ReadI2C();
StopI2C();
return Id;
}
uint16_t BMP180_Temperature_Lecture(void)
{
uint16_t value;
uint8_t prueba1, prueba2;
StartI2C();
WriteI2C(BMP180_W);
WriteI2C(0xF4);
WriteI2C(0x2E);
StopI2C();
__delay_ms(5);
WriteI2C(BMP180_W);
WriteI2C(0xF6);
RestartI2C();
WriteI2C(BMP180_R);
prueba1=ReadI2C();//Value in F6
i2c_master_ack(I2C_master_ACK);
prueba2=ReadI2C();//Value in F7
i2c_master_ack(I2C_master_NOACK);
StopI2C();
value = prueba1<<8 | prueba2;
return value;
}
void i2c_master_ack(unsigned char ack_type)
{
SSPCON2bits.ACKDT = ack_type; // 1 = Not Acknowledge, 0 = Acknowledge
SSPCON2bits.ACKEN = 1; // Enable Acknowledge
while (SSPCON2bits.ACKEN == 1);
}
I send the data via Serial to an Arduino and this prints it on the computer. Almost always when I try to print it, it prints only "2", and sometimes, he writes the sentence and prints the data from the module which contains the BMP180. But most of the time, what Arduino shows me is: 22222222...
I have tested setting the MCLR = OFF, but it is still the same.
The lanes SDA and SCL have a resistance of 4.7k to 3.5V. The module is powered with 3.5V, but the pic with 5V.
I do not think that the problem is too high intensity with this module.
Am I maybe forgetting something?
It is curious that when I touch the pic (when I try to change some wire or something) it automatically resets.
Thank you very much.
Manuel.
Sounds like you have some serious noise issues there. There doesn't seem to be anything wrong with your code at first glance. When an unused pin is left floating, it acts like a high-impedance antenna and basically picks up on any and all signals in the vicinity, whatever they may be. Power consumption also rises dramatically in this case. This may explain why the PIC resets every time you touch it. Based on personal experience, I suggest you do some or all of the following:
Set all your unused pins as digital outputs and pull them down to gnd using the LAT register. For example, if you're not using your RA2 pin, put this at the beginning of your main function:
TRISAbits.RA2 = 0;
LATAbits.LATA2 = 0;
Do this for all your unused pins. It will decrease your power consumption dramatically.
Make sure you've connected the right resistance value between the 5V(in your case) and the reset pin. There are issues sometimes if the value is higher or lower than recommended in the datasheet.
Measure the PIC power supply voltage on the corresponding pins. You could do this with a multimeter, but I recommend that you use an oscilloscope if you have access to one. Hook it up to the power supply pins of the PIC and measure the value while you're sending data over the I2C. There could be a drop in the supply voltage, which causes a brown-out reset, i.e. the voltage drops below 3V in your case and the controller resets. If that's the case, clearing the BOREN bit would be a possible solution, however, such power drops indicate a bigger problem with your circuit, so I suggest you look into it some more.

Resources