I am using the dsPIC33EP (MC) microcontroller via the high speed PWM module to create PWM frequencies between around 30-120Khz. On each PWM interrupt, I need to sample 5 ADC channels (However, I have 9 in total - the other 4 are not critical to be sampled within the ADC interrupt).
The issue I am having, is on the first PWM interrupt, only AN0 is sampled, on the second interrupt, only AN1 is sampled etc... So, currently it takes 9 PWM periods to sample all the ADC channels.
I use MPLAB MCC to initialise the PWM and ADC modules,
Has anyone had this issue before and know how to get around it?
(Note - I change the AD1CON2bits.SMPI to 9 somewhere else in the code so I dont read the CTMU and CTMU Temp channels)
Thanks
/* PWM init */
// PCLKDIV 1;
PTCON2 = 0x00;
// PTPER 4915;
PTPER = 0x1333;
// SEVTCMP 0;
SEVTCMP = 0x00;
// MDC 0;
MDC = 0x00;
// CHOPCLK 0; CHPCLKEN disabled;
CHOP = 0x00;
// PWMKEY 0;
PWMKEY = 0x00;
// MDCS Master; FLTIEN disabled; CAM Edge Aligned; DTC Dead-time function is disabled; TRGIEN enabled; XPRES disabled; ITB Master; IUE disabled; CLIEN disabled; MTBS disabled; DTCP disabled;
PWMCON1 = 0x580;
// MDCS Master; FLTIEN disabled; CAM Edge Aligned; DTC Dead-time function is disabled; TRGIEN disabled; XPRES disabled; ITB Master; IUE disabled; CLIEN disabled; MTBS disabled; DTCP disabled;
PWMCON2 = 0x180;
// MDCS Master; FLTIEN disabled; CAM Edge Aligned; DTC Dead-time function is disabled; TRGIEN disabled; XPRES disabled; ITB Master; IUE disabled; CLIEN disabled; MTBS disabled; DTCP disabled;
PWMCON3 = 0x180;
//FLTDAT PWM1L Low, PWM1H Low; SWAP disabled; OVRENH enabled; PENL enabled; PMOD Redundant Output Mode; OVRENL enabled; OSYNC disabled; POLL enabled; PENH enabled; CLDAT PWM1L Low, PWM1H Low; OVRDAT PWM1L Low, PWM1H Low; POLH disabled;
__builtin_write_PWMSFR(&IOCON1, 0xD700, &PWMKEY);
//FLTDAT PWM2L Low, PWM2H Low; SWAP disabled; OVRENH enabled; PENL enabled; PMOD Redundant Output Mode; OVRENL enabled; OSYNC disabled; POLL enabled; PENH enabled; CLDAT PWM2L Low, PWM2H Low; OVRDAT PWM2L Low, PWM2H Low; POLH disabled;
__builtin_write_PWMSFR(&IOCON2, 0xD700, &PWMKEY);
//FLTDAT PWM3L Low, PWM3H Low; SWAP disabled; OVRENH enabled; PENL enabled; PMOD Redundant Output Mode; OVRENL enabled; OSYNC disabled; POLL enabled; PENH enabled; CLDAT PWM3L Low, PWM3H Low; OVRDAT PWM3L Low, PWM3H Low; POLH disabled;
__builtin_write_PWMSFR(&IOCON3, 0xD700, &PWMKEY);
//FLTPOL disabled; CLPOL disabled; CLSRC FLT1; CLMOD disabled; FLTMOD Fault input is disabled; FLTSRC FLT32;
__builtin_write_PWMSFR(&FCLCON1, 0xFB, &PWMKEY);
//FLTPOL disabled; CLPOL disabled; CLSRC FLT1; CLMOD disabled; FLTMOD Fault input is disabled; FLTSRC FLT32;
__builtin_write_PWMSFR(&FCLCON2, 0xFB, &PWMKEY);
//FLTPOL disabled; CLPOL disabled; CLSRC FLT1; CLMOD disabled; FLTMOD Fault input is disabled; FLTSRC FLT32;
__builtin_write_PWMSFR(&FCLCON3, 0xFB, &PWMKEY);
// PDC1 0;
PDC1 = 0x00;
// PDC2 0;
PDC2 = 0x00;
// PDC3 0;
PDC3 = 0x00;
// PHASE1 0;
PHASE1 = 0x00;
// PHASE2 0;
PHASE2 = 0x00;
// PHASE3 0;
PHASE3 = 0x00;
// DTR1 0;
DTR1 = 0x00;
// DTR2 0;
DTR2 = 0x00;
// DTR3 0;
DTR3 = 0x00;
// ALTDTR1 0;
ALTDTR1 = 0x00;
// ALTDTR2 0;
ALTDTR2 = 0x00;
// ALTDTR3 0;
ALTDTR3 = 0x00;
// TRGCMP 0;
TRIG1 = 0x00;
// TRGCMP 0;
TRIG2 = 0x00;
// TRGCMP 0;
TRIG3 = 0x00;
// TRGDIV 1; TRGSTRT 0;
TRGCON1 = 0x00;
// TRGDIV 1; TRGSTRT 0;
TRGCON2 = 0x00;
// TRGDIV 1; TRGSTRT 0;
TRGCON3 = 0x00;
// BPLL disabled; BPHH disabled; BPLH disabled; BCH disabled; FLTLEBEN disabled; PLR disabled; CLLEBEN disabled; BCL disabled; PLF disabled; PHR disabled; BPHL disabled; PHF disabled;
LEBCON1 = 0x00;
// BPLL disabled; BPHH disabled; BPLH disabled; BCH disabled; FLTLEBEN disabled; PLR disabled; CLLEBEN disabled; BCL disabled; PLF disabled; PHR disabled; BPHL disabled; PHF disabled;
LEBCON2 = 0x00;
// BPLL disabled; BPHH disabled; BPLH disabled; BCH disabled; FLTLEBEN disabled; PLR disabled; CLLEBEN disabled; BCL disabled; PLF disabled; PHR disabled; BPHL disabled; PHF disabled;
LEBCON3 = 0x00;
// LEB 0;
LEBDLY1 = 0x00;
// LEB 0;
LEBDLY2 = 0x00;
// LEB 0;
LEBDLY3 = 0x00;
// CHOPLEN disabled; CHOPHEN disabled; BLANKSEL No state blanking; CHOPSEL No state blanking;
AUXCON1 = 0x00;
// CHOPLEN disabled; CHOPHEN disabled; BLANKSEL No state blanking; CHOPSEL No state blanking;
AUXCON2 = 0x00;
// CHOPLEN disabled; CHOPHEN disabled; BLANKSEL No state blanking; CHOPSEL No state blanking;
AUXCON3 = 0x00;
/* Initialize PWM Generator Interrupt Handler*/
PWM_SetGenerator1InterruptHandler(&PWM_Generator1_CallBack);
IFS5bits.PWM1IF = false;
IEC5bits.PWM1IE = true;
// SYNCOEN disabled; SEIEN disabled; SESTAT disabled; SEVTPS 1; SYNCSRC SYNCI1; SYNCEN disabled; PTSIDL disabled; PTEN disabled; EIPU enabled; SYNCPOL disabled;
PTCON = 0x400;
/* ADC init */
// ASAM enabled; ADDMABM disabled; ADSIDL disabled; DONE disabled; SIMSAM Sequential; FORM Absolute decimal result, unsigned, right-justified; SAMP disabled; SSRC PWM1 Primary Trigger; AD12B 12-bit; ADON enabled; SSRCG enabled;
AD1CON1 = 0x8414;
// CSCNA enabled; VCFG0 AVDD; VCFG1 AVSS; ALTS disabled; BUFM disabled; SMPI Generates interrupt after completion of every 11th sample/conversion operation; CHPS 1 Channel;
AD1CON2 = 0x428;
// SAMC 0; ADRC FOSC/2; ADCS 7;
AD1CON3 = 0x07;
// CH0SA AN0; CH0SB AN0; CH0NB AVSS; CH0NA AVSS;
AD1CHS0 = 0x00;
// CSS26 disabled; CSS25 disabled; CSS24 disabled; CSS31 enabled; CSS30 enabled;
AD1CSSH = 0xC000;
// CSS2 enabled; CSS1 enabled; CSS0 enabled; CSS8 enabled; CSS7 enabled; CSS6 enabled; CSS5 enabled; CSS4 enabled; CSS3 enabled;
AD1CSSL = 0x1FF;
// DMABL Allocates 1 word of buffer to each analog input; ADDMAEN disabled;
AD1CON4 = 0x00;
// CH123SA disabled; CH123SB CH1=OA2/AN0,CH2=AN1,CH3=AN2; CH123NA disabled; CH123NB CH1=VREF-,CH2=VREF-,CH3=VREF-;
AD1CHS123 = 0x00;
//Assign Default Callbacks
ADC1_SetInterruptHandler(&ADC1_CallBack);
Related
i'm currently trying to use a button as an interrupt. But i'm struggling with it. I've seen in the datasheet that i should enable: INTE bit of PIE0, INTEDG of INTCON, AND GIE if PIE.
I've also seen that i have to use the INTPPS register to configure a pin to be the interrupt, so as i want to use RB0 as an interupt, then i used PIR0bits.INTF to check if the interupt= true in my ISR. I did that :
void ledLoop(unsigned char *ptr){
unsigned char *tmp = ptr;
while(*tmp<64){
LATB = *tmp;
*tmp *= 2;
__delay_ms(200);
}
}
void __interrupt() high_isr(void){
INTCONbits.GIE = 0;
if( PIR0bits.INTF){
LATB = ~LATB;
PIR0bits.INTF = 0;
}
INTCONbits.GIE = 1;
}
void launch(void){
unsigned char run = 1;
while(1){
if(PORTDbits.RD7==1){
unsigned char tmp = 1;
ledLoop(&tmp);
}
if(PORTDbits.RD6==1){
LATB=0xff;
}
else{
LATB=0;
}
}
}
void main(void)
{
ANSELDbits.ANSD7=0;
ANSELDbits.ANSD6=0;
TRISB=0;
TRISA=0;
LATA=0x00;
TRISDbits.TRISD7=1;
TRISDbits.TRISD6=1;
TRISBbits.TRISB0=1;
//GIE: Global Interrupt Enable bit
//PEIE: Peripheral Interrupt Enable bit
PIE0bits.INTE = 1;
INTCONbits.GIE = 1;
INTCONbits.INTEDG = 1;
INTPPSbits.INTPPS = 0x08;
launch();
}
Any idea of what is wrong ?
using :
MPLABX IDE
XC8 Compiler
PIC16F18875
There are three thing wrong with the code you posted:
No configuration words, so code will not build as posted
Changing the GIE bit inside of an ISR
PORTB pin RB0 not configured for digital input
See my comments using /* */ for your code fixed:
/*
* File: main.c
* Author: dan1138
*
* Created on September 24, 2020, 2:23 PM
*/
// PIC16F18875 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1) (FOSC = 32 MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out reset disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config WRT = OFF // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF // DataNVM code protection bit (Data EEPROM code protection 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 (32000000ul)
void ledLoop(unsigned char *ptr){
unsigned char *tmp = ptr;
while(*tmp<64){
LATB = *tmp;
*tmp *= 2;
__delay_ms(200);
}
}
void __interrupt() high_isr(void){
//INTCONbits.GIE = 0; /* <= do not do this in an interrupt service routine */
if((PIE0bits.INTE) && ( PIR0bits.INTF)){ /* check that the interrupt is enabled AND asserted */
LATB = ~LATB;
PIR0bits.INTF = 0;
}
//INTCONbits.GIE = 1; /* <= do not do this in an interrupt service routine */
}
void launch(void){
unsigned char run = 1;
while(1){
if(PORTDbits.RD7==1){
unsigned char tmp = 1;
ledLoop(&tmp);
}
if(PORTDbits.RD6==1){
LATB=0xff;
}
else{
LATB=0;
}
}
}
void main(void)
{
ANSELDbits.ANSD7=0;
ANSELDbits.ANSD6=0;
TRISB=0;
TRISA=0;
LATA=0x00;
TRISDbits.TRISD7=1;
TRISDbits.TRISD6=1;
TRISBbits.TRISB0=1;
ANSELBbits.ANSB0=0; /* make RB0 a digital input */
//GIE: Global Interrupt Enable bit
//PEIE: Peripheral Interrupt Enable bit
PIE0bits.INTE = 1;
INTCONbits.GIE = 1;
INTCONbits.INTEDG = 1;
INTPPSbits.INTPPS = 0x08; /* assign INT input to RB0 */
launch();
}
I am unable to compile bellow code in platfromIO.
But which is successfully compiled in arduino 1.5 IDE
#define PRESSED LOW
#define NOT_PRESSED HIGH
const byte led = 13;
#define shortPress 100
#define longPress 500
long blinkInterval = 500;
unsigned long previousBlink=0;
bool ledState = true;
bool blinkState = true;
typedef struct Buttons {
const byte pin = 2;
const int debounce = 10;
unsigned long counter=0;
bool prevState = NOT_PRESSED;
bool currentState;
} Button;
// create a Button variable type
Button button;
void setup() {
pinMode(led, OUTPUT);
pinMode(button.pin, INPUT_PULLUP);
}
void loop() {
// check the button
button.currentState = digitalRead(button.pin);
// has it changed?
if (button.currentState != button.prevState) {
delay(button.debounce);
// update status in case of bounce
button.currentState = digitalRead(button.pin);
if (button.currentState == PRESSED) {
// a new press event occured
// record when button went down
button.counter = millis();
}
if (button.currentState == NOT_PRESSED) {
// but no longer pressed, how long was it down?
unsigned long currentMillis = millis();
//if ((currentMillis - button.counter >= shortPress) && !(currentMillis - button.counter >= longPress)) {
if ((currentMillis - button.counter >= shortPress) && !(currentMillis - button.counter >= longPress)) {
// short press detected.
handleShortPress();
}
if ((currentMillis - button.counter >= longPress)) {
// the long press was detected
handleLongPress();
}
}
// used to detect when state changes
button.prevState = button.currentState;
}
blinkLED();
}
void handleShortPress() {
blinkState = true;
ledState = true;
blinkInterval = blinkInterval / 2;
if (blinkInterval <= 50)
blinkInterval = 500;
}
void handleLongPress() {
blinkState = false;
ledState = false;
}
void blinkLED() {
// blink the LED (or don't!)
if (blinkState) {
if (millis() - previousBlink >= blinkInterval) {
// blink the LED
ledState = !ledState;
previousBlink = millis();
}
} else {
ledState = false;
}
digitalWrite(led, ledState);
}
Here is the platformio.ini
[env:attiny13]
platform = atmelavr
board = attiny13
framework = arduino
Compile output of PIO
Processing attiny13 (platform: atmelavr; board: attiny13; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/attiny13.html
PLATFORM: Atmel AVR 1.15.0 > Generic ATtiny13
HARDWARE: ATTINY13 1MHz, 64B RAM, 1KB Flash
PACKAGES: toolchain-atmelavr 1.50400.190710 (5.4.0), framework-arduinoavr 4.1.2
LDF: Library Dependency Finder -> xxxxxx
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Compiling .pio/build/attiny13/src/Blink.cpp.o
src/Blink.cpp: In function 'void setup()':
src/Blink.cpp:33:23: error: 'INPUT_PULLUP' was not declared in this scope
pinMode(button.pin, INPUT_PULLUP);
^
*** [.pio/build/attiny13/src/Blink.cpp.o] Error 1
============================================================================ [FAILED] Took 1.31 seconds
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Do I need to amend the code for PlatromIO ?
However when I comment on the INPUT_PULLUP line. I am able to compile in PIO but output binary size is higher with compared to Arduino IDE ?
Can you suggest any flags or configurations to optimize the binary size ?
I am having a hard time properly using USB HID buffers, and want to send the scroll lock key when I press the button.
I have tried sending what I thought was scroll lock (0x47) and it sends the ctrl, left alt, and right alt keys instead.
I used the table here for reference: https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf
This is loosely based off of http://mitchtech.net/arduino-usb-hid-keyboard/
/* Arduino USB Keyboard HID demo
* Cut/Copy/Paste Keys
*/
#define SCRLCK 0x47
//#define KEY_LEFT_SHIFT 0x02
//#define KEY_RIGHT_CTRL 0x10
//#define KEY_RIGHT_SHIFT 0x20
uint8_t buf[8] = {
0 }; /* Keyboard report buffer */
#define PIN_PTT 2
//#define PIN_CUT 6
//#define PIN_PASTE 7
int state = 1;
int prevState;
void setup()
{
Serial.begin(9600);
pinMode(PIN_PTT, INPUT);
//pinMode(PIN_CUT, INPUT);
//pinMode(PIN_PASTE, INPUT);
// Enable internal pull-ups
digitalWrite(PIN_PTT, 1);
//digitalWrite(PIN_CUT, 1);
//digitalWrite(PIN_PASTE, 1);
delay(200);
}
void loop()
{
state = digitalRead(PIN_PTT);
if (prevState != state) {
buf[0] = SCRLCK; // Scroll Lock
// buf[2] = 27; // Letter X
// buf[2] = 123; // Cut key: Less portable
Serial.write(buf, 8); // Send keypress
prevState = state;
releaseKey();
}
}
void releaseKey()
{
buf[0] = 0;
Serial.write(buf, 8); // Release key
delay(100);
}
I expect scroll lock to be sent, but upon using passmark KeyboardTest I see that ctrl, left alt, and right alt are sent instead.
You should send the first key code starting at offset 2:
buf[2] = SCRLCK;
Offset 0 is used for key modifier flags like ctrl, alt, etc.
I am trying to verify the Arduino code in ADS1198 and Arduino Due. It comes the error show 'Serial1' does not name a type' even I defined two serial port in the beginning(where I gave the comments now). How to deal with this error 'Serial1' does not name a type'. how can I define these two port on ArduinoDue so that complieling successfully.
#include <ads1298.h>
#include <Base64.h>
// Minimal sketch for connection to ADS129n family. Load this script and open Tools/SerialMonitor.
// You should see text like this
// Device Type (ID Control Register): 62 Channels: 8
// If you see "Channels: 0" then check your wiring
#include "ads1298.h"
#include "adsCMD.h"
#include <Base64.h>
#include <SPI.h> // include the SPI library:
int gMaxChan = 0; //maximum number of channels supported by ads129n = 4,6,8
int gIDval = 0; //Device ID : lower 5 bits of ID Control Register
int activeSerialPort = 0; //data will be sent to serial port that last sent commands. E.G. bluetooth or USB port
const int kPIN_LED = 13; //pin with in-built light - typically 13, 11 for Teensy 2.0.
//ADSCMD
#include "Arduino.h"
//For Leonardo SPI see http://openenergymonitor.blogspot.com/2012/06/arduino-leonardo-atmega32u4-and-rfm12b.html
//constants define pins on Arduino
// Arduino Due
const int IPIN_PWDN = 47; //not required for TI demo kit
const int PIN_CLKSEL = 49; //6;//*optional
const int IPIN_RESET = 48; //*optional
const int PIN_START = 46;
const int IPIN_DRDY = 45;
const int IPIN_CS = 52;
const int PIN_DOUT = 11; //SPI out
const int PIN_DIN = 12; //SPI in
const int PIN_SCLK = 13; //SPI clock
//
//function prototypes
void adc_wreg(int reg, int val); //write register
void adc_send_command(int cmd); //send command
int adc_rreg(int reg); //read register
//start Serial Peripheral Interface
int numActiveChannels = 0;
boolean gActiveChan[9]; // reports whether channels 1..9 are active
boolean isRdatac = false;
boolean base64Mode = false;
int sampleCount=0;
boolean isLimit=false;
char hexDigits[] = "0123456789ABCDEF";
uint8_t serialBytes[200];
char sampleBuffer[1000];
uint8_t chan1[2];
const char *hardwareType = "unknown";
const char *boardName = "HackEEG";
const char *makerName = "Hamid, Mujahid, Abdul Hameed";
const char *driverVersion = "ADS1298 driver v0.1";
#if defined(__SAM3X8E__)
//#define isDUE //Detect Arduino Due
//#define WiredSerial SerialUSB //Use Due's Native port
//#define NSerial SerialUSB
#endif
void setup(){
using namespace ADS1298;
//prepare pins to be outputs or inputs
pinMode(PIN_SCLK, OUTPUT); //optional - SPI library will do this for us
pinMode(PIN_DIN, OUTPUT); //optional - SPI library will do this for us
pinMode(PIN_DOUT, INPUT); //optional - SPI library will do this for us
pinMode(IPIN_CS, OUTPUT);
pinMode(PIN_START, OUTPUT);
pinMode(IPIN_DRDY, INPUT);
pinMode(PIN_CLKSEL, OUTPUT); // *optional
pinMode(IPIN_RESET, OUTPUT); // *optional
pinMode(IPIN_PWDN, OUTPUT); // *optional
digitalWrite(PIN_CLKSEL, HIGH); // External clock
//start Serial Peripheral Interface
SPI.begin();
SPI.setBitOrder(MSBFIRST);
#ifndef isDUE
SPI.setClockDivider(SPI_CLOCK_DIV4); //forum.pjrc.com/.../1156-Teensy-3-SPI-Basic-Clock-Questions
#endif
SPI.setDataMode(SPI_MODE1);
//Start ADS1298
delay(500); //wait for the ads129n to be ready - it can take a while to charge caps
digitalWrite(PIN_CLKSEL, HIGH); // External clock
delay(10); // wait for oscillator to wake up
delay(1);
digitalWrite(IPIN_PWDN, HIGH); // *optional - turn off power down mode
digitalWrite(IPIN_RESET, HIGH);
delay(1000);// *optional
digitalWrite(IPIN_RESET, LOW);
delay(1);// *optional
digitalWrite(IPIN_RESET, HIGH);
delay(1500); // *optional Wait for 18 tCLKs AKA 9 microseconds, we use 1 millisecond
adc_send_command(SDATAC); // Send SDATAC Command (Stop Read Data Continuously mode)
// delayMicroseconds(2);
delay(100);
// Determine model number and number of channels available
gIDval = adc_rreg(ID); //lower 5 bits of register 0 reveal chip type
switch (gIDval & B00011111 ) { //least significant bits reports channels
case B10000: //16
hardwareType = "ADS1294";
gMaxChan = 4; //ads1294
break;
case B10001: //17
hardwareType = "ADS1296";
gMaxChan = 6; //ads1296
break;
case B10010: //18
hardwareType = "ADS1298";
gMaxChan = 8; //ads1298
break;
case B11110: //30
hardwareType = "ADS1299";
gMaxChan = 8; //ads1299
break;
case B10110: //22
hardwareType = "ADS1198";
gMaxChan = 8; //ads1198
break;
default:
gMaxChan = 0;
}
}
void detectActiveChannels() { //set device into RDATAC (continous) mode -it will stream data//
if ((isRdatac) || (gMaxChan < 1)) return; //we can not read registers when in RDATAC mode
//Serial.println("Detect active channels: ");
using namespace ADS1298;
numActiveChannels = 0;
for (int i = 1; i <= gMaxChan; i++) {
delayMicroseconds(1);
int chSet = adc_rreg(CHnSET + i);
gActiveChan[i] = ((chSet & 7) != SHORTED);
if ( (chSet & 7) != SHORTED) numActiveChannels ++;
}
}
//start serial port
Serial1.begin(115200);
Serial.begin(115200); //use native port on Due
Serial1.begin(115200) and Serial.begin(115200) need to be placed in a function. Put them in setup().
HI I'm trying to implement simple spi communication to turn on 8 led connected to shift register can amy one help me whats wrong with this code, im getting random output, I want to send 0x02 to turn on led(00000010) .
#include <xc.h>
#include "spi.h"
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit
#define _XTAL_FREQ 8000000
void main()
{
char data = 0x10;
spiInit(SPI_MASTER_OSC_DIV4, SPI_DATA_SAMPLE_END, SPI_CLOCK_IDLE_LOW, SPI_IDLE_2_ACTIVE);
TRISC0 = 0;
RC0 = 1;
RC3 = 0;
RC5 = 0;
while(1)
{
RC0 = 0; //Slave Select
__delay_ms(100);
spiWrite(data);
while(!SSPSTATbits.BF);
__delay_ms(100);
RC0= 1; //Slave Deselect
}
}