Toute le code est à le bas de cette web page:
Todo el codigo se encuentra en la parte inferior de esta pagina web:
***********************************//////////////////*********************************************************************
Version 51:
Date:
Software:
Hardware:
Resultats:
***********************************//////////////////*********************************************************************
***********************************//////////////////*********************************************************************
Version 49:
Date:
Software:
Hardware:
Resultats:
***********************************//////////////////*********************************************************************
***********************************//////////////////*********************************************************************
Version 44:
Date:
Software:
Hardware:
Resultats:
***********************************//////////////////*********************************************************************
***********************************//////////////////*********************************************************************
Version 42:
Date:
Software:
Hardware:
Resultats:
***********************************//////////////////*********************************************************************
***********************************//////////////////*********************************************************************
Version 37:
Date:
Software:
Hardware:
Resultats:
***********************************//////////////////*********************************************************************
***********************************//////////////////*********************************************************************
Version 32:
Date:
Software:
Hardware:
Resultats:
***********************************//////////////////*********************************************************************
***********************************//////////////////*********************************************************************
Version 12:
Date:
Software:
Hardware:
***********************************//////////////////*********************************************************************
Mon version 25 dspic33fj128gp802 + c30 + GLK19264-7T-1U-FGW + PLLPRE +PLLDIV + PLLPOST + 19200 + RS232 :
Regarder la prochen WEB page: http://www.maxim-ic.com/datasheet/index.mvp/id/1068
/**********************************************************************
// NOM DU PROGRAMME
// Programme Prototype des controle des moteurs et des capteurs de présence
// Mon version 25 dspic33fj128gp802 + c30 + GLK19264-7T-1U-FGW + PLLPRE +PLLDIV + PLLPOST :
// AUTEUR:
// Estefan Mauricio Gomez
//
// DATE DE CRÉATION: 22 Marz 2011
//
//
**********************************************************************/
#include "p33FJ128GP802.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <outcompare.h>
#include <timer.h>
#include <uart.h>
#if defined (__dsPIC33F__)
_FGS(GSS_OFF & GWRP_OFF);//GSS_OFF code protect off/Write Protect:GWRP_OFF Disabled
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF);//Primary oscillator (XT, HS, EC) w/ PLL//** Two-speed Oscillator Startup :IESO_OFF Disabled
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_NONE); // this sets I/O on OSC2 pin
_FWDT(FWDTEN_OFF);
#define __dsPIC33F__ 1
#endif
#define UART_TX_PIN_NORMAL 0xF7FF /* UART TX pin operates normally */
#define UART_TX_PIN_LOW 0xFFFF /* UART TX pin driven low */
//If PLLPRE<4:0> = 0, then N1 = 2. This yields a
//VCO input of 10/2 = 5 MHz, which is within the
//acceptable range of 0.8-8 MHz.
//• If PLLDIV<8:0> = 0x1E, then M = 32. This yields a
//VCO output of 5 x 32 = 160 MHz, which is within
//the 100-200 MHz ranged needed.
//• If PLLPOST<1:0> = 0, then N2 = 2. This provides
//a Fosc of 160/2 = 80 MHz. The resultant device
//operating speed is 80/2 = 40 MIPS.
#define N1 2
#define ValueN1 0b00000 //_PLLPRE = ValueN1; // PLL Phase Detector Input Divider bits (N1)
//CLKDIVbits.PLLPPRE = ValueN1; // N2 = 2
#define M 50
#define ValueM 0b000110000 //_PLLDIV = ValueM; // PLL divider (M=50)
#define N2 4
#define ValueN2 0b01 //_PLLPOST = ValueN2; // PLL VCO Output Divider Select bits (N2)
//CLKDIVbits.PLLPOST = ValueN2; // N2 = 2
#define FIN 7370000LL // clock-frequecy in Hz with suffix LL (64-bit-long), eg. 32000000LL for 32MHz
// Configure Oscillator to operate the device at 40 MHz
// Fosc = Fin * M/(N1 * N2), Fcy = Fosc/2
// Fosc = 7370000 * 50/(2 * 4) = 46 062 500Hz for FRC_PLL input clock
#define FOSC (FIN * (M/(N1*N2)))
#define FCY (FOSC/2) // MCU is running at FCY MIPS
#define BAUDRATE 19200
#define BRGVAL 74;//((FCY/BAUDRATE)/16)-1=73,97151693 Aprox 74
#define PERIODE 1850
#ifndef __DELAY_H
#define delay_us(x) __delay32(((x*FCY)/1000000L)) // delays x us
#define delay_ms(x) __delay32(((x*FCY)/1000L)) // delays x ms
#define __DELAY_H 1
#include <libpic30.h>
#endif
void OSC_init(void)
{
//_PLLPRE = ValueN1;
//_PLLPOST = ValueN2; // PLL VCO Output Divider Select bits (N2)
// // PLL Phase Detector Input Divider bits (N1)
//_PLLDIV = ValueM;
CLKDIVbits.PLLPRE = ValueN1;
PLLFBDbits.PLLDIV = ValueM; // PLL divider (M=2)
CLKDIVbits.PLLPOST = ValueN2;
CLKDIVbits.DOZE=0b011; ////011 = FCY/8 (default)
OSCTUN = 0; // Tune FRC oscillator, if FRC is used
RCONbits.SWDTEN = 0; // Disable Watch Dog Timer
while(OSCCONbits.LOCK != 1) {}; // Wait for PLL to lock
U1MODEbits.STSEL = 0; // 1 Stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8 data bits
U1MODEbits.ABAUD = 0; // Auto-Baud Disabled
U1MODEbits.BRGH = 0; // Low Speed mode
U1BRG = 74;//BRGVAL; // BAUD Rate Setting for 19200
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART TX
}
void mogd_font (int font_id) {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x31);
while(U1STAbits.UTXBF==1);
WriteUART1 (font_id);
while(U1STAbits.UTXBF==1);
}
void mogd_clear (void) {
WriteUART1 (0xfe);
//while(U1STAbits.UTXBF==1);
WriteUART1 (0x58);
//while(U1STAbits.UTXBF==1);
}
void mogd_home (void) {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x48);
while(U1STAbits.UTXBF==1);
}
void mogd_goto (int col, int row) {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x47);
while(U1STAbits.UTXBF==1);
WriteUART1 (col);
while(U1STAbits.UTXBF==1);
WriteUART1 (row);
while(U1STAbits.UTXBF==1);
}
void mogd_gotoxy (int col, int row) {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x47);
while(U1STAbits.UTXBF==1);
WriteUART1 (col);
while(U1STAbits.UTXBF==1);
WriteUART1 (row);
while(U1STAbits.UTXBF==1);
}
void mogd_drawline (int x1, int x2, int y1, int y2) {
WriteUART1 (0xfe); // x = 0-31, y = 0-121
while(U1STAbits.UTXBF==1);
WriteUART1 (0x6c);
while(U1STAbits.UTXBF==1);
WriteUART1 (x1);
while(U1STAbits.UTXBF==1);
WriteUART1 (x2);
while(U1STAbits.UTXBF==1);
WriteUART1 (y1);
while(U1STAbits.UTXBF==1);
WriteUART1 (y2);
while(U1STAbits.UTXBF==1);
}
void mogd_contline (int x, int y) {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x65);
while(U1STAbits.UTXBF==1);
WriteUART1 (x);
while(U1STAbits.UTXBF==1);
WriteUART1 (y);
while(U1STAbits.UTXBF==1);
}
void mogd_pixel (int x, int y) {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x65);
while(U1STAbits.UTXBF==1);
WriteUART1 (x);
while(U1STAbits.UTXBF==1);
WriteUART1 (y);
while(U1STAbits.UTXBF==1);
}
void mogd_setcolor (int color) { // Set drawing color
WriteUART1 (0xfe); // 0 = White, 255 = Black
while(U1STAbits.UTXBF==1);
WriteUART1 (0x63);
while(U1STAbits.UTXBF==1);
WriteUART1 (color);
while(U1STAbits.UTXBF==1);
}
void mogd_drawrect (int type, int color, int x1, int x2, int y1, int y2) {
if (type == 0) { // Type 0 = Outline, 1 = Solid
WriteUART1 (0xfe); // Color 0 = White, 255 = Black
while(U1STAbits.UTXBF==1);
WriteUART1 (0x72);
while(U1STAbits.UTXBF==1);
WriteUART1 (color);
while(U1STAbits.UTXBF==1);
WriteUART1 (x1);
while(U1STAbits.UTXBF==1);
WriteUART1 (x2);
while(U1STAbits.UTXBF==1);
WriteUART1 (y1);
while(U1STAbits.UTXBF==1);
WriteUART1 (y2);
while(U1STAbits.UTXBF==1);
}
else {
WriteUART1 (0xfe); // GPIO = 1 or 2
while(U1STAbits.UTXBF==1);
WriteUART1 (0x78);
while(U1STAbits.UTXBF==1);
WriteUART1 (color);
while(U1STAbits.UTXBF==1);
WriteUART1 (x1);
while(U1STAbits.UTXBF==1);
WriteUART1 (x2);
while(U1STAbits.UTXBF==1);
WriteUART1 (y1);
while(U1STAbits.UTXBF==1);
WriteUART1 (y2);
while(U1STAbits.UTXBF==1);
}
}
void mogd_bargraph_init (int ref, int type, int x1, int x2, int y1, int y2) {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x67);
while(U1STAbits.UTXBF==1);
WriteUART1 (ref); // Ref = 0-15
while(U1STAbits.UTXBF==1);
WriteUART1 (type); // Type 0 = Vertical Upwards
while(U1STAbits.UTXBF==1);
WriteUART1 (x1); // Type 1 = Horizontal Rightwards
while(U1STAbits.UTXBF==1);
WriteUART1 (x2); // Type 2 = Vertical Downwards
while(U1STAbits.UTXBF==1);
WriteUART1 (y1); // Type 3 = Horizontal Leftwards
while(U1STAbits.UTXBF==1);
WriteUART1 (y2); // x/y = Placement/Size (Pixels)
while(U1STAbits.UTXBF==1);
}
void mogd_bargraph (int ref, int value) {
WriteUART1 (0xfe); // Ref = 0-15
while(U1STAbits.UTXBF==1);
WriteUART1 (0x67); // Value (Pixels, see bargraph_init)
while(U1STAbits.UTXBF==1);
WriteUART1 (ref);
while(U1STAbits.UTXBF==1);
WriteUART1 (value);
while(U1STAbits.UTXBF==1);
}
void mogd_backlight (int state, int minutes) {
if (state == 0) { // 0 = Off, 1 = On,
WriteUART1 (0xfe); // If minutes = 0, then continuous on
while(U1STAbits.UTXBF==1);
WriteUART1 (0x46);
while(U1STAbits.UTXBF==1);
}
else {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x42);
while(U1STAbits.UTXBF==1);
WriteUART1(minutes);
while(U1STAbits.UTXBF==1);
}
}
void mogd_gpio (int state, int gpio) {
if (state == 0) { // 0 = Off, 1 = On,
WriteUART1 (0xfe); // GPIO = 1 or 2
while(U1STAbits.UTXBF==1);
WriteUART1 (0x56);
while(U1STAbits.UTXBF==1);
WriteUART1 (gpio);
while(U1STAbits.UTXBF==1);
}
else {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x57);
while(U1STAbits.UTXBF==1);
WriteUART1 (gpio);
while(U1STAbits.UTXBF==1);
}
}
void mogd_autoscroll (int state) {
if (state == 0) { // 0 = Off, 1 = On,
WriteUART1 (0xfe); // GPIO = 1 or 2
while(U1STAbits.UTXBF==1);
WriteUART1 (0x52);
while(U1STAbits.UTXBF==1);
}
else {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x51);
while(U1STAbits.UTXBF==1);
}
}
void mogd_contrast (int value, int save) {
if (save == 0) { // Save 0 = No, 1 = Yes
WriteUART1 (0xfe); // Value = 0-255
while(U1STAbits.UTXBF==1);
WriteUART1 (0x50);
while(U1STAbits.UTXBF==1);
WriteUART1 (value);
while(U1STAbits.UTXBF==1);
}
else {
WriteUART1 (0xfe);
while(U1STAbits.UTXBF==1);
WriteUART1 (0x91);
while(U1STAbits.UTXBF==1);
WriteUART1 (value);
while(U1STAbits.UTXBF==1);
}
}
//#define _TRISD3 TRISDbits.TRISD3
unsigned int temp_count;
/* Allocation memoire des buffers et drivers */
void Delay( unsigned int delay_count )
{
temp_count = delay_count +1;
asm volatile("outer: dec _temp_count");
asm volatile("cp0 _temp_count");
asm volatile("bra z, done");
asm volatile("do #3200, inner" );
asm volatile("nop");
asm volatile("inner: nop");
asm volatile("bra outer");
asm volatile("done:");
}
int read_adc1=2500;
int read_adc2=2500;
int read_adc3=2500;
int read_adc4=2500;
int Canal=1;
int suma1=1850;
int suma2=1850;
int suma3=1850;
void Init_ADC( int amask )
{
AD1PCFGL = amask;//select channel
//bit 10 AD12B: 10-Bit or 12-Bit Operation Mode bit
//1 = 12-bit, 1-channel ADC operation
//0 = 10-bit, 4-channel ADC operation
//bit 7-5 SSRC<2:0>: Sample Clock Source Select bits
//111 = Internal counter ends sampling and starts conversion (auto-convert)
//110 = Reserved
//101 = Reserved
//100 = GP timer (Timer5 for ADC1) compare ends sampling and starts conversion
//011 = Reserved
//010 = GP timer (Timer3 for ADC1) compare ends sampling and starts conversion
//001 = Active transition on INT0 pin ends sampling and starts conversion
//000 = Clearing sample bit ends sampling and starts conversion
AD1CON1 = 0b0000010011100000; // automatic conversion start after sampling
//AD1CON1 = 0b0000010000000000; // automatic conversion start after sampling
AD1CSSL = 0b11111111; // scanning required
//bit 10 CSCNA: Scan Input Selections for CH0+ during Sample A bit
//1 = Scan inputs
//0 = Do not scan inputs
//bit 9-8 CHPS<1:0>: Selects Channels Utilized bits
//When AD12B = 1, CHPS<1:0> is: U-0, Unimplemented, Read as ‘0’
//1x = Converts CH0, CH1, CH2 and CH3
//01 = Converts CH0 and CH1
//00 = Converts CH0
AD1CON2 = 0b0000000000000000;
AD1CON3 = 0b0001111100000010; // Tsamp = 32 x Tad; Tad=125ns
AD1CON1bits.ADON = 1; // turn on the ADC
}
int readADC( int ch)
{
Delay(1);
AD1CHS0 = ch; // 1. select analog input channel
AD1CON1bits.SAMP = 1; // 2. start sampling
Delay(1);
while (!AD1CON1bits.DONE); // 5. wait for the conversion to complete
return ADC1BUF0; // 6. read the conversion result
} // readADC
int writeSPI1( int data)
{
SPI1BUF = data; // write to buffer for TX
while( !SPI1STATbits.SPIRBF); // wait for transfer to complete
return SPI1BUF; // read the received value
}//writeSPI2
void _ISR __attribute__((__no_auto_psv__)) _T2Interrupt(void) {
TMR2 = 0;
int i;
int j;
int k;
int ch_sel;
char mystring[12]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
PORTBbits.RB5=0; //pour le pwm
for(i=0; i<=(read_adc1+suma1);i++) // sets up next output for each pin
{
PORTBbits.RB5=1;//pour le pwm
}
PORTBbits.RB5=0;//pour le pwm
Delay(1);
PORTBbits.RB6=0; //pour le pwm
for(j=0; j<=(read_adc2+suma2);j++) // sets up next output for each pin
{
PORTBbits.RB6=1;//pour le pwm
}
PORTBbits.RB6=0;//pour le pwm
Delay(1);
PORTBbits.RB7=1; //pour le pwm
for(k=0; k<=(read_adc3+suma3);k++) // sets up next output for each pin
{
PORTBbits.RB7=1;//pour le pwm
}
PORTBbits.RB7=0;//pour le pwm
Delay(1);
//T2IF: Timer2 Interrupt Flag Status bit //1 = Interrupt request has occurred
//0 = Interrupt request has not occurred
IFS0bits.T2IF = 0;
ch_sel=0b00000001;
Init_ADC(ch_sel);
read_adc1=readADC(ch_sel);
ch_sel=0b00000100;
Init_ADC(ch_sel);
read_adc2=readADC(ch_sel);
ch_sel=0b00000101;
Init_ADC(ch_sel);
read_adc3=readADC(ch_sel);
ch_sel=0b00000000;
Init_ADC(ch_sel);
read_adc4=readADC(ch_sel);
read_adc3=4095-read_adc3;
sprintf(mystring,"<%i>",read_adc4);
PORTBbits.RB15=0;//pour le SPI enable
writeSPI1( mystring[1]);
writeSPI1( mystring[2]);
writeSPI1( mystring[3]);
writeSPI1( mystring[4]);
for(k=0; k<=1000;k++) // sets up next output for each pin
{
}
PORTBbits.RB15=1;
//mogd_clear();
delay_ms(2);
while(U1STAbits.UTXBF==1);
//mogd_pixel(1,1);
char Txdata[] = {'H','o','l','a','D','e','n','y','\0'};
putsUART1 ((unsigned int *)Txdata);
while(U1STAbits.UTXBF==1);
}
#define SPI_MASTER 0x0120
#define SPI_ENABLE 0x8000
volatile unsigned char adc_lcd_update;
int main (void)
{
OSC_init();
//OpenUART1(U1MODEvalue, U1STAvalue, BRGVAL);
TRISBbits.TRISB14=0;//salida SPI CLK hacia display
TRISBbits.TRISB13=0;//salida SPI hacia display
TRISBbits.TRISB12=1;//1 entrada SPI
TRISBbits.TRISB6=0;
TRISBbits.TRISB7=0;
//TRISBbits.TRISB8=0;//je ne pu pas utilize ca parce que je besoin du SDA1, et SCL1
//TRISBbits.TRISB9=0;//je ne pu pas utilize ca parce que je besoin du SDA1, et SCL1
TRISBbits.TRISB5=0;
TRISBbits.TRISB6=0;
TRISBbits.TRISB2=1;//entrada A/D conversor
TRISBbits.TRISB3=1;//entrada A/D conversor
TRISAbits.TRISA0=1;//entrada A/D conversor
TRISAbits.TRISA1=1;//entrada A/D conversor
//SPI ramappable pic selection
_RP13R=0b00111;//pin 13 RPn tied to SPI1 Data Output
RPINR20bits.SDI1R=0xc; //SPI1 Data Input SDI1 RPINR20 SDI1R<4:0> pin 23 rp12
_RP14R=0b01000; //SCK1 01000 RPn tied to SPI1 Clock Output
SPI1CON1 = SPI_MASTER;
SPI1STAT = SPI_ENABLE; // enable the peripheral
SPI1CON1bits.PPRE=0b11;
//Serial UART
_RP9R=0b00011;//U1TX 00011 RPn tied to UART1 Transmit pin 18 RP9
RPINR18bits.U1RXR=0x8;//UART1 Receive U1RX RPINR18 U1RXR<4:0> pin 17 RP8
PORTBbits.RB15=1;
/* Initialisation du timer 2 */
T2CONbits.TON= 1;// active timer1
T2CONbits.TCKPS = 3;// bit 5-4 TCKPS<1:0>: Timer1 Input Clock Prescale Select bits
//11 = 1:256
T2CONbits.TON= 1;// active timer1
T2CONbits.TCS= 0; // Slectionne la source
PR2 =PERIODE;// période
// Clear counter
T2CONbits.TON= 1;// active timer2
TMR2 = 0;
IPC1bits.T2IP = 2;// Interrupt priority 2 (med-low)
IEC0bits.T2IE = 1;// active interrupt
/*******************************************/
//Init_ADC();
PORTBbits.RB15=0;
writeSPI1( 0x76);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
PORTBbits.RB15=0;
writeSPI1( 0x7a);
writeSPI1( 0x00);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
while(1)
{
}
}//main
//FIN Vesion 25 Estable
Code avec interrupt pwm+adc+dspic33fj128gp802+spi display spark fun
codigo con interrupciones + pwm+adc+dspic33fj128gp802+spi display spark fun
code with interruption pwm+adc+dspic33fj128gp802+spi display spark fun
/********************************************************************
// NOM DU PROGRAMME
// Programme Prototype des controle des moteurs et des capteurs de présence
//
// AUTEUR:
// Estefan Apablaza-Arancibia + Mauricio Gomez + Code SPI Isabel Provost.
//
// DATE DE CRÉATION: 1 février 2011
//
//
**********************************************************************/
//#include <p33Fxxxx.h>
#include "p33FJ128GP802.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<outcompare.h>
#include<timer.h>
#ifndef __DELAY_H
#define FOSC 48000000LL // clock-frequecy in Hz with suffix LL (64-bit-long), eg. 32000000LL for 32MHz
#define FCY (FOSC/2) // MCU is running at FCY MIPS
#define delay_us(x) __delay32(((x*FCY)/1000000L)) // delays x us
#define delay_ms(x) __delay32(((x*FCY)/1000L)) // delays x ms
#define __DELAY_H 1
#include <libpic30.h>
#endif
char mystring[6]={' ',' ',' ',' ',' ',' '};
#if defined (__dsPIC33F__)
// set chip configuration bits
_FGS(GSS_OFF & GWRP_OFF);
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_NONE); // this sets I/O on OSC2 pin
_FWDT(FWDTEN_OFF);
#endif
#define _TRISD3 TRISDbits.TRISD3
/* Allocation memoire des buffers et drivers */
int i;
int read_adc=60;
void _ISR __attribute__((__no_auto_psv__)) _T1Interrupt(void) {
TMR1 = 0;
PORTBbits.RB6 =~ PORTBbits.RB6;
IFS0bits.T1IF = 0;
}
void _ISR __attribute__((__no_auto_psv__)) _T2Interrupt(void) {
TMR2 = 0;
OC1RS = read_adc; // Write Duty Cycle value for next PWM cycle
PORTAbits.RA0 =~ PORTAbits.RA0;
IFS0bits.T2IF = 0;
}
unsigned int temp_count;
void Delay( unsigned int delay_count )
{
temp_count = delay_count +1;
asm volatile("outer: dec _temp_count");
asm volatile("cp0 _temp_count");
asm volatile("bra z, done");
asm volatile("do #3200, inner" );
asm volatile("nop");
asm volatile("inner: nop");
asm volatile("bra outer");
asm volatile("done:");
}
#define SPI_MASTER 0x0120
#define SPI_ENABLE 0x8000
int writeSPI1( int data)
{
SPI1BUF = data; // write to buffer for TX
while( !SPI1STATbits.SPIRBF); // wait for transfer to complete
return SPI1BUF; // read the received value
}//writeSPI2
void Init_ADC( void )
{
/* set port configuration here */
AD1PCFGLbits.PCFG1 = 0; // set AN1 to analog input
/* set channel scanning here, auto sampling and convert,
with default read-format mode */
AD1CON1 = 0x00E4;
/* select 12-bit, 1 channel ADC operation */
AD1CON1bits.AD12B = 1;
/* enable DMA mode (ADC module sends all results
to ADBUF0 and interrupts on each result */
// ADCON1bits.ADDMAEN = 1;
/* No channel scan for CH0+, Use MUX A,
SMPI = 1 per interrupt, Vref = AVdd/AVss */
AD1CON2 = 0x0000;
/* Set Samples and bit conversion time */
AD1CON3 = 0x032F;
/* set channel scanning here for AN4 and AN5 */
AD1CSSL = 0x0000;
/* // channel select AN5
AD1CHS0 = 0x0005;*/
/* reset ADC interrupt flag */
IFS0bits.AD1IF = 0;
/* enable ADC interrupts, disable this interrupt if the DMA is enabled */
IEC0bits.AD1IE = 1;
/* turn on ADC module */
AD1CON1bits.ADON = 1;
}
/*---------------------------------------------------------------------
Function Name: ADCInterrupt
Description: ADC Interrupt Handler
Inputs: None
Returns: None
-----------------------------------------------------------------------*/
volatile unsigned char adc_lcd_update;
void __attribute__((__interrupt__)) _ADC1Interrupt( void )
{
read_adc = ADC1BUF0;
/* reset ADC interrupt flag */
IFS0bits.AD1IF = 0;
}
int main (void)
{
TRISBbits.TRISB15=0;//0 salida
TRISBbits.TRISB14=0;
TRISBbits.TRISB13=0;
TRISBbits.TRISB12=1;//1 entrada
TRISBbits.TRISB6=0;
_RP6R=0b10010;
_RP13R=0b00111;//pin 13 RPn tied to SPI1 Data Output
RPINR20bits.SDI1R=0xc; //SPI1 Data Input SDI1 RPINR20 SDI1R<4:0> pin 23 rp12
_RP14R=0b01000; //SCK1 01000 RPn tied to SPI1 Clock Output
PORTBbits.RB15=1;
SPI1CON1 = SPI_MASTER;
SPI1STAT = SPI_ENABLE; // enable the peripheral
SPI1CON1bits.PPRE=0b01;
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
_RP6R=0b10010;
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
OC1R = 150; // Write the duty cycle for the first PWM pulse
OC1RS = 200; // Write the duty cycle for the second PWM pulse
OC1CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC1R = 150; // Load the Compare Register Value
OC1CONbits.OCM = 0b110; // Select the Output Compare mode
/* Initialisation du timer 1 */
T1CONbits.TON= 0;// active timer1
T1CONbits.TCKPS = 2;// 1:1 prescaler
T1CONbits.TON= 1;// active timer1
T1CONbits.TCS= 0; // Slectionne la source
PR1 =18;// période 100 us
// Clear counter
T1CONbits.TON= 1;// active timer1
TMR1 = 0;
/* Initialisation du timer 2 */
T2CONbits.TON= 1;// active timer1
T2CONbits.TCKPS = 3;// bit 5-4 TCKPS<1:0>: Timer1 Input Clock Prescale Select bits
//11 = 1:256
T2CONbits.TON= 1;// active timer1
T2CONbits.TCS= 0; // Slectionne la source
PR2 =1825;// période
// Clear counter
T2CONbits.TON= 1;// active timer2
TMR2 = 0;
IPC0bits.T1IP = 2;// Interrupt priority 2 (low)
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;// active interrupt
IFS0bits.T2IF = 0;
IEC0bits.T2IE = 1;// active interrupt
/*******************************************/
Init_ADC();
PORTBbits.RB15=0;
writeSPI1( 0x76);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
PORTBbits.RB15=0;
writeSPI1( 0x7a);
writeSPI1( read_adc);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
while(1)
{
AD1CON1bits.SAMP=1;
AD1CHS0 = 1;
sprintf(mystring,"<%01d>",read_adc);
PORTBbits.RB15=0;
writeSPI1( mystring[1]);
writeSPI1( mystring[2]);
writeSPI1( mystring[3]);
writeSPI1( mystring[4]);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
/*PORTBbits.RB15=0;
writeSPI1( 0x02);
writeSPI1( 0x78);
Delay(1);
PORTBbits.RB15=1;
Delay(2);*/
}
}//main
Code sans interrupt pwm+adc+dspic33fj128gp802+spi display spark fun
codigo sin interrupciones + pwm+adc+dspic33fj128gp802+spi display spark fun
code without interruption pwm+adc+dspic33fj128gp802+spi display spark fun
Cette code marche meilleur que le anterior
/**********************************************************************
// NOM DU PROGRAMME
// Programme Prototype des controle des moteurs et des capteurs de présence
//
// AUTEUR:
// Estefan Apablaza-Arancibia + Mauricio Gomez + Code SPI Isabel Provost
//
// DATE DE CRÉATION: 15 février 2011
// dsPIC33FJ128GP802-10.c
//
**********************************************************************/
//#include <p33Fxxxx.h>
#include "p33FJ128GP802.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<outcompare.h>
#include<timer.h>
#ifndef __DELAY_H
#define FOSC 48000000LL // clock-frequecy in Hz with suffix LL (64-bit-long), eg. 32000000LL for 32MHz
#define FCY (FOSC/2) // MCU is running at FCY MIPS
#define delay_us(x) __delay32(((x*FCY)/1000000L)) // delays x us
#define delay_ms(x) __delay32(((x*FCY)/1000L)) // delays x ms
#define __DELAY_H 1
#include <libpic30.h>
#endif
char mystring[6]={' ',' ',' ',' ',' ',' '};
#if defined (__dsPIC33F__)
// set chip configuration bits
_FGS(GSS_OFF & GWRP_OFF);
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_NONE); // this sets I/O on OSC2 pin
_FWDT(FWDTEN_OFF);
#endif
#define _TRISD3 TRISDbits.TRISD3
/* Allocation memoire des buffers et drivers */
int i;
int read_adc=60;
void _ISR __attribute__((__no_auto_psv__)) _T1Interrupt(void) {
TMR1 = 0;
PORTBbits.RB6 =~ PORTBbits.RB6;
IFS0bits.T1IF = 0;
}
int suma=1000;
void _ISR __attribute__((__no_auto_psv__)) _T2Interrupt(void) {
TMR2 = 0;
OC1RS = read_adc; // Write Duty Cycle value for next PWM cycle
PORTBbits.RB8 =~ PORTBbits.RB8;
PORTBbits.RB9=1;
for(i=0; i<read_adc+suma;i++) // sets up next output for each pin
{
PORTBbits.RB9=0;
}
PORTBbits.RB9=1;
IFS0bits.T2IF = 0;
}
unsigned int temp_count;
void Delay( unsigned int delay_count )
{
temp_count = delay_count +1;
asm volatile("outer: dec _temp_count");
asm volatile("cp0 _temp_count");
asm volatile("bra z, done");
asm volatile("do #3200, inner" );
asm volatile("nop");
asm volatile("inner: nop");
asm volatile("bra outer");
asm volatile("done:");
}
#define SPI_MASTER 0x0120
#define SPI_ENABLE 0x8000
int writeSPI1( int data)
{
SPI1BUF = data; // write to buffer for TX
while( !SPI1STATbits.SPIRBF); // wait for transfer to complete
return SPI1BUF; // read the received value
}//writeSPI2
void Init_ADC( void )
{
/* set port configuration here */
AD1PCFGLbits.PCFG1 = 0; // set AN1 to analog input
/* set channel scanning here, auto sampling and convert,
with default read-format mode */
AD1CON1 = 0x00E4;
/* select 12-bit, 1 channel ADC operation */
AD1CON1bits.AD12B = 1;
/* enable DMA mode (ADC module sends all results
to ADBUF0 and interrupts on each result */
// ADCON1bits.ADDMAEN = 1;
/* No channel scan for CH0+, Use MUX A,
SMPI = 1 per interrupt, Vref = AVdd/AVss */
AD1CON2 = 0x0000;
/* Set Samples and bit conversion time */
AD1CON3 = 0x032F;
/* set channel scanning here for AN4 and AN5 */
AD1CSSL = 0x0000;
/* // channel select AN5
AD1CHS0 = 0x0005;*/
/* reset ADC interrupt flag */
IFS0bits.AD1IF = 0;
/* enable ADC interrupts, disable this interrupt if the DMA is enabled */
IEC0bits.AD1IE = 1;
/* turn on ADC module */
AD1CON1bits.ADON = 1;
}
/*---------------------------------------------------------------------
Function Name: ADCInterrupt
Description: ADC Interrupt Handler
Inputs: None
Returns: None
-----------------------------------------------------------------------*/
volatile unsigned char adc_lcd_update;
void __attribute__((__interrupt__)) _ADC1Interrupt( void )
{
read_adc = ADC1BUF0;
if ( read_adc<100) read_adc=100;
if ( read_adc>3100) read_adc=3100;
/* reset ADC interrupt flag */
PORTBbits.RB7 =~ PORTBbits.RB7;
IFS0bits.AD1IF = 0;
}
int main (void)
{
TRISBbits.TRISB15=0;//0 salida
TRISBbits.TRISB14=0;
TRISBbits.TRISB13=0;
TRISBbits.TRISB12=1;//1 entrada
TRISBbits.TRISB6=0;
TRISBbits.TRISB7=0;
TRISBbits.TRISB8=0;
TRISBbits.TRISB9=0;
_RP6R=0b10010;
_RP13R=0b00111;//pin 13 RPn tied to SPI1 Data Output
RPINR20bits.SDI1R=0xc; //SPI1 Data Input SDI1 RPINR20 SDI1R<4:0> pin 23 rp12
_RP14R=0b01000; //SCK1 01000 RPn tied to SPI1 Clock Output
PORTBbits.RB15=1;
SPI1CON1 = SPI_MASTER;
SPI1STAT = SPI_ENABLE; // enable the peripheral
SPI1CON1bits.PPRE=0b01;
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
_RP6R=0b10010;
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
OC1R = 150; // Write the duty cycle for the first PWM pulse
OC1RS = 200; // Write the duty cycle for the second PWM pulse
OC1CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC1R = 150; // Load the Compare Register Value
OC1CONbits.OCM = 0b110; // Select the Output Compare mode
/* Initialisation du timer 1 */
T1CONbits.TON= 0;// active timer1
T1CONbits.TCKPS = 1;// 1:64 prescaler
T1CONbits.TON= 1;// active timer1
T1CONbits.TCS= 0; // Slectionne la source
PR1 =18;// période 100 us
// Clear counter
T1CONbits.TON= 1;// active timer1
TMR1 = 0;
T1CONbits.TSYNC=1;
/* Initialisation du timer 2 */
T2CONbits.TON= 1;// active timer1
T2CONbits.TCKPS = 3;// bit 5-4 TCKPS<1:0>: Timer1 Input Clock Prescale Select bits
//11 = 1:256
T2CONbits.TON= 1;// active timer1
T2CONbits.TCS= 0; // Slectionne la source
PR2 =1825;// période
// Clear counter
T2CONbits.TON= 1;// active timer2
TMR2 = 0;
IPC0bits.T1IP = 3;// Interrupt priority 3 (med-high)
IPC1bits.T2IP = 2;// Interrupt priority 2 (med-low)
IPC3bits.AD1IP = 1; //Interrupt priority 1 (low)
// IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;// active interrupt
// IFS0bits.T2IF = 0;
IEC0bits.T2IE = 1;// active interrupt
IEC1bits.T4IE = 1;// active interrupt
IEC1bits.T5IE = 1;// active interrupt
/*******************************************/
Init_ADC();
PORTBbits.RB15=0;
writeSPI1( 0x76);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
PORTBbits.RB15=0;
writeSPI1( 0x7a);
writeSPI1( 0x00);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
while(1)
{
AD1CON1bits.SAMP=1;
AD1CHS0 = 1;
sprintf(mystring,"<%01d>",read_adc);
PORTBbits.RB15=0;
writeSPI1( mystring[1]);
writeSPI1( mystring[2]);
writeSPI1( mystring[3]);
writeSPI1( mystring[4]);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
/*PORTBbits.RB15=0;
writeSPI1( 0x02);
writeSPI1( 0x78);
Delay(1);
PORTBbits.RB15=1;
Delay(2);*/
}
}//main
Aqui en la tercera version de mi codigo, puedo manejar varios servomotores con varias conversiones de ADC
Ici, dans la troisième version de mon code, je peux gérer plusieurs PWMs avec des plusieurs conversions ADC
/**********************************************************************
// NOM DU PROGRAMME
// Programme Prototype des controle des moteurs et des capteurs de présence
// dsPIC33FJ128GP802-12.c
// AUTEUR:
// Estefan Apablaza-Arancibia - Mauricio Gomez
//
// DATE DE CRÉATION: 25 février 2011
//
//
**********************************************************************/
//#include <p33Fxxxx.h>
#include "p33FJ128GP802.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<outcompare.h>
#include<timer.h>
#ifndef __DELAY_H
#define FOSC 48000000LL // clock-frequecy in Hz with suffix LL (64-bit-long), eg. 32000000LL for 32MHz
#define FCY (FOSC/2) // MCU is running at FCY MIPS
#define delay_us(x) __delay32(((x*FCY)/1000000L)) // delays x us
#define delay_ms(x) __delay32(((x*FCY)/1000L)) // delays x ms
#define __DELAY_H 1
#include <libpic30.h>
#endif
#if defined (__dsPIC33F__)
// set chip configuration bits
_FGS(GSS_OFF & GWRP_OFF);
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_NONE); // this sets I/O on OSC2 pin
_FWDT(FWDTEN_OFF);
#endif
//#define _TRISD3 TRISDbits.TRISD3
unsigned int temp_count;
/* Allocation memoire des buffers et drivers */
void Delay( unsigned int delay_count )
{
temp_count = delay_count +1;
asm volatile("outer: dec _temp_count");
asm volatile("cp0 _temp_count");
asm volatile("bra z, done");
asm volatile("do #3200, inner" );
asm volatile("nop");
asm volatile("inner: nop");
asm volatile("bra outer");
asm volatile("done:");
}
int read_adc1=2500;
int read_adc2=2500;
int read_adc3=2500;
int read_adc4=2500;
int Canal=1;
int suma1=1400;
int suma2=1400;
int suma3=1400;
void Init_ADC( int amask )
{
AD1PCFGL = amask;//select channel
//bit 10 AD12B: 10-Bit or 12-Bit Operation Mode bit
//1 = 12-bit, 1-channel ADC operation
//0 = 10-bit, 4-channel ADC operation
//bit 7-5 SSRC<2:0>: Sample Clock Source Select bits
//111 = Internal counter ends sampling and starts conversion (auto-convert)
//110 = Reserved
//101 = Reserved
//100 = GP timer (Timer5 for ADC1) compare ends sampling and starts conversion
//011 = Reserved
//010 = GP timer (Timer3 for ADC1) compare ends sampling and starts conversion
//001 = Active transition on INT0 pin ends sampling and starts conversion
//000 = Clearing sample bit ends sampling and starts conversion
AD1CON1 = 0b0000010011100000; // automatic conversion start after sampling
//AD1CON1 = 0b0000010000000000; // automatic conversion start after sampling
AD1CSSL = 0b11111111; // scanning required
//bit 10 CSCNA: Scan Input Selections for CH0+ during Sample A bit
//1 = Scan inputs
//0 = Do not scan inputs
//bit 9-8 CHPS<1:0>: Selects Channels Utilized bits
//When AD12B = 1, CHPS<1:0> is: U-0, Unimplemented, Read as ‘0’
//1x = Converts CH0, CH1, CH2 and CH3
//01 = Converts CH0 and CH1
//00 = Converts CH0
AD1CON2 = 0b0000000000000000;
AD1CON3 = 0b0001111100000010; // Tsamp = 32 x Tad; Tad=125ns
AD1CON1bits.ADON = 1; // turn on the ADC
}
int readADC( int ch)
{
Delay(1);
AD1CHS0 = ch; // 1. select analog input channel
AD1CON1bits.SAMP = 1; // 2. start sampling
Delay(1);
while (!AD1CON1bits.DONE); // 5. wait for the conversion to complete
return ADC1BUF0; // 6. read the conversion result
} // readADC
int writeSPI1( int data)
{
SPI1BUF = data; // write to buffer for TX
while( !SPI1STATbits.SPIRBF); // wait for transfer to complete
return SPI1BUF; // read the received value
}//writeSPI2
void _ISR __attribute__((__no_auto_psv__)) _T2Interrupt(void) {
TMR2 = 0;
int i;
int j;
int k;
int ch_sel;
char mystring[12]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
PORTBbits.RB9=0; //pour le pwm
for(i=0; i<=(read_adc1+suma1);i++) // sets up next output for each pin
{
PORTBbits.RB9=1;//pour le pwm
}
PORTBbits.RB9=0;//pour le pwm
Delay(1);
PORTBbits.RB8=0; //pour le pwm
for(j=0; j<=(read_adc2+suma2);j++) // sets up next output for each pin
{
PORTBbits.RB8=1;//pour le pwm
}
PORTBbits.RB8=0;//pour le pwm
Delay(1);
PORTBbits.RB7=1; //pour le pwm
for(k=0; k<=(read_adc3+suma3);k++) // sets up next output for each pin
{
PORTBbits.RB7=1;//pour le pwm
}
PORTBbits.RB7=0;//pour le pwm
Delay(1);
//T2IF: Timer2 Interrupt Flag Status bit //1 = Interrupt request has occurred
//0 = Interrupt request has not occurred
IFS0bits.T2IF = 0;
ch_sel=0b00000001;
Init_ADC(ch_sel);
read_adc1=readADC(ch_sel);
ch_sel=0b00000100;
Init_ADC(ch_sel);
read_adc2=readADC(ch_sel);
ch_sel=0b00000101;
Init_ADC(ch_sel);
read_adc3=readADC(ch_sel);
ch_sel=0b00000000;
Init_ADC(ch_sel);
read_adc4=readADC(ch_sel);
read_adc3=4095-read_adc3;
sprintf(mystring,"<%i>",read_adc4);
PORTBbits.RB15=0;//pour le SPI enable
writeSPI1( mystring[1]);
writeSPI1( mystring[2]);
writeSPI1( mystring[3]);
writeSPI1( mystring[4]);
for(k=0; k<=500;k++) // sets up next output for each pin
{
}
PORTBbits.RB15=1;
}
#define SPI_MASTER 0x0120
#define SPI_ENABLE 0x8000
volatile unsigned char adc_lcd_update;
int main (void)
{
I2C1CONbits.I2CEN=0;
TRISBbits.TRISB15=0;//0 salida
TRISBbits.TRISB14=0;//salida SPI CLK hacia display
TRISBbits.TRISB13=0;//salida SPI hacia display
TRISBbits.TRISB12=1;//1 entrada SPI
TRISBbits.TRISB6=0;
TRISBbits.TRISB7=0;
TRISBbits.TRISB8=0;
TRISBbits.TRISB9=0;
TRISBbits.TRISB2=1;//entrada A/D conversor
TRISBbits.TRISB3=1;//entrada A/D conversor
TRISAbits.TRISA0=1;//entrada A/D conversor
TRISAbits.TRISA1=1;//entrada A/D conversor
//SPI ramappable pic selection
_RP13R=0b00111;//pin 13 RPn tied to SPI1 Data Output
RPINR20bits.SDI1R=0xc; //SPI1 Data Input SDI1 RPINR20 SDI1R<4:0> pin 23 rp12
_RP14R=0b01000; //SCK1 01000 RPn tied to SPI1 Clock Output
SPI1CON1 = SPI_MASTER;
SPI1STAT = SPI_ENABLE; // enable the peripheral
SPI1CON1bits.PPRE=0b11;
PORTBbits.RB15=1;
/* Initialisation du timer 2 */
T2CONbits.TON= 1;// active timer1
T2CONbits.TCKPS = 3;// bit 5-4 TCKPS<1:0>: Timer1 Input Clock Prescale Select bits
//11 = 1:256
T2CONbits.TON= 1;// active timer1
T2CONbits.TCS= 0; // Slectionne la source
PR2 =1825;// période
// Clear counter
T2CONbits.TON= 1;// active timer2
TMR2 = 0;
IPC1bits.T2IP = 2;// Interrupt priority 2 (med-low)
IEC0bits.T2IE = 1;// active interrupt
/*******************************************/
//Init_ADC();
PORTBbits.RB15=0;
writeSPI1( 0x76);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
PORTBbits.RB15=0;
writeSPI1( 0x7a);
writeSPI1( 0x00);
Delay(1);
PORTBbits.RB15=1;
Delay(2);
while(1)
{
}
}//main