Hello,
I want to use SPI bus to control a VGA (Variable Gain Amplifier). I'm using a DAC between the C8051F120 micro-controller and the VGA. I want to control the input voltage of the VGA, so the output voltage of the DAC. To control this, I'm using too two push-button to increment or decrement the output voltage of the DAC. I've maked the source code but there are some problems about the SPI outputs and the interrupts /INT0 and /INT1. Here my progam:
//------------------------------------------------------------------------------ // Includes //------------------------------------------------------------------------------ #include <stdio.h> #include <c8051f120.h> // SFR declarations //------------------------------------------------------------------------------ // Global constants //------------------------------------------------------------------------------ #define DAC 16 // 10000 in binary sbit SPI_SCLK=P0^2; // SPI clock on P0.2 sbit SPI_MISO=P0^3; // Master In/Slave Out on P0.3 sbit SPI_MOSI=P0^4; // Master Out/Slave In on P0.4 sbit SPI_NSS=P0^5; // Slave Select on P0.5 sbit INC=P1^0; // /INT0 interrupt on P1.0 sbit DEC=P1^1; // /INT1 interrupt on P1.1 sbit TEST=P2^0; //------------------------------------------------------------------------------ // Global variables //------------------------------------------------------------------------------ int valeur = 0; // transfert data //------------------------------------------------------------------------------ // Functions prototypes //------------------------------------------------------------------------------ void clk_init (void); void port_init (void); void config_SPI (void); void transfert_donnees (int donnee); void interupt_decrement (void); void interupt_increment (void); //------------------------------------------------------------------------------ // Main //------------------------------------------------------------------------------ void main (void) { clk_init (); port_init (); config_SPI (); transfert_donnees (valeur); while (1) { } } //------------------------------------------------------------------------------ // Clock Initialisation //------------------------------------------------------------------------------ void clk_init (void) { int i; char sfrpage_save = SFRPAGE; // Save current page of SFR SFRPAGE = CONFIG_PAGE; // Load config_page OSCXCN = 0x67; // Start external oscillator with 22.1184MHz crystal for(i=0; i < 256; i++); // Wait for the oscillator to start up while(!(OSCXCN & 0x80)); CLKSEL = 0x01; OSCICN = 0x00; SFRPAGE = CONFIG_PAGE; PLL0CN = 0x04; SFRPAGE = LEGACY_PAGE; FLSCL = 0x10; SFRPAGE = CONFIG_PAGE; PLL0CN |= 0x01; PLL0DIV = 0x04; PLL0FLT = 0x01; PLL0MUL = 0x09; for(i=0; i < 256; i++); PLL0CN |= 0x02; while(!(PLL0CN & 0x10)); CLKSEL = 0x02; SFRPAGE = sfrpage_save; // Restore SFR page } //------------------------------------------------------------------------------ // Ports I/O initialisation //------------------------------------------------------------------------------ void port_init (void) { char sfrpage_save = SFRPAGE; // Save current page of SFR SFRPAGE = CONFIG_PAGE; // Load config_page EA = 1; // Enable all interrupts TCON = 0x0F; // Enable extern interrupts WDTCN = 0xde; WDTCN = 0xad; // Disable watchdog timer XBR0 = 0x02; // Enable SPI bus XBR1 = 0x14; // Enable /INT0 and /INT1 XBR2 = 0x40; // Enable Crossbar and weak pull-up P0MDOUT |= 0x3C; // P0.5, P0.4, P0.3 and P0.2 are outputs (for SPI datas) P0 = 0x3C; P1MDIN &= 0x03; // P1.0 and P1.1 are inputs (for interrupts) EX0 = 1; EX1 = 1; SFRPAGE = sfrpage_save; // Restore SFR page } //------------------------------------------------------------------------------ // SPI bus configuration //------------------------------------------------------------------------------ void config_SPI (void) { char sfrpage_save = SFRPAGE; // Save current SFR page SFRPAGE = SPI0_PAGE; // Load SPI page SPI0CFG |= 0x40; // Master Mode Enable SPI0CN |= 0x89; // Slave select and enable SPI SPI0CKR = 0x00; // SYSCLK/2 SFRPAGE = sfrpage_save; // Restore SFR page } //------------------------------------------------------------------------------ // Data transfert //------------------------------------------------------------------------------ void transfert_donnees (int data) { unsigned char data_1; unsigned char data_2; data_1 = ( data & 0xFF00) >> 8; // MSB in data_1 data_2 = ( data & 0x00FF); // LSB in data_2 SPI0DAT = data_1; // data_1 transmission SPIF = 0; SPI0DAT = data_2; // data_2 transmission SPIF = 0; } //------------------------------------------------------------------------------ // Interrupt routines //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ // /INT0 interrupt //------------------------------------------------------------------------------ void interupt_increment (void) interrupt 0 { if ( valeur >= 1200) { valeur = 1200; // Stability at 1200 } else { valeur = valeur + DAC; // increment of 10000 (binary) } transfert_donnees (valeur); } //------------------------------------------------------------------------------ // /INT1 interrupt //------------------------------------------------------------------------------ void interupt_decrement (void) interrupt 2 { if ( valeur <= 0) { valeur = 0; // Not negative; } else { valeur = valeur - DAC; // decrement of 10000 (binary) } transfert_donnees (valeur); }
Help me please.
Thanks