⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dactst.c

📁 * Use 10 MHz crystal frequency. * Use Timer0 for ten millisecond looptime. * Blink "Alive" LED e
💻 C
字号:

#include "config.h"

#include "serial.c"
#include "serio.c"
#include "i2cmsu.c"

// read value from ADC, output to serial port via printf 
// also output to DAC on I2C Bus 

#define DAC 0x58        // I2C DAC 01011000 


int update_dac(unsigned char val) {

  i2c_start();
  i2c_put(DAC);
  i2c_put(0x00);
  i2c_put(val);
  i2c_stop();
}


void main(void)
{
  int adc_value;
  char dac_value;
  char inchar;

  serial_init(95,1); // 19200 in HSPLL mode, crystal = 7.3728 MHz 

  if (POR == 0){  // bit 2 in RCON register, cleared to 0 on power-up reset
    POR = 1;  // setting to bit to 1 means that will remain a '1' for MCLR reset
    printf("Power-on reset has occurred.");
    pcrlf();
  }
  if (TO == 0) {
    SWDTEN = 0; // disable watchdog timer
    printf("Watchdog timer reset has occurred, press reset to recover.\n");
    pcrlf();
    while(1);
  }
  SWDTEN = 1;  // enable watchdog timer

  i2c_init(17); // about 400Khz  for HSPLL mode, crystal = 7.3728 MHz 

  // ADC Setup 
  TRISA = 0xFF;		// all bits input 
  //ADCON1 = 0x8E, ADCON0 = 0x80; by bit assignments below
  ADFM = 1;             // right justification           
  // A0 analog input, others digital, Vref+ = VDD, Vref- = Vss
  PCFG3 = 1; PCFG2 = 1; PCFG1 = 1; PCFG0 = 0;
  ADCS2 = 0; ADCS1 = 1; ADCS0 = 0; // ADC clock = Fsoc/32
  CHS2 = 0; CHS1 = 0; CHS0 = 0;   // select channel 0
  ADON = 1; // turn on ADC

  printf("ADC is configured!!!");
  pcrlf();

  printf("Hit any key to start ");
  pcrlf();
  inchar=getch();

  while(1) {

    GODONE = 1;
    while (GODONE);  // wait for end of conversion 

    // read result 
    adc_value = 0;
	// for (ADRESH << 8) to work in MCC18, must have integer promotions turned on
    adc_value = adc_value | (ADRESH * 256);  
    adc_value = adc_value | (ADRESL);
    printf("%x",adc_value);
    pcrlf();
    dac_value = ((adc_value >> 2)) & 0x00ff;
    // now write to DAC 
   update_dac(dac_value);
    
  }

}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -