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

📄 f_13_27_ledpwm_tst.c

📁 PIC_Hi-Tech_C_Mike_Pearces_I2C_routines Microchi PIC_Hi-Tech C_I2C_for_EEPROM_24LC01B Code
💻 C
字号:
#include "config.h"
#include "serial.c"
#include "serio.c"


#if defined(__18CXX)
void my_gets (char *s);
//return a string from the console
void my_gets (char *s){
 char c;
 
 do {
  c = getche();
  *s = c;
  s++;
  } while (c != '\r');
}
#endif

//generates a PWM square wave using timer 2 and the CCP module 
// prompts user for PR2 value 
unsigned int dc_val;
void main(void){
  serial_init(95,1); // 19200 in HSPLL mode, crystal = 7.3728 MHz 
  // configure timer 2
  //T2CKPS1 = 1; // pre scale of 16 
  T2CKPS1 = 0; // pre scale of 16 
  T2CKPS0 = 1; // pre scale of 4
  // configure PWM
  PR2 = 254;// set PR2 to max value
  CCPR1L = (255 >> 1);     // 50% duty cycle 
  //clear lower 2 bits of duty cycle
  bitclr(CCP1CON, 5);
  bitclr(CCP1CON, 4);
  bitclr(TRISC,2);// set CCP1 output 

  // Set PWM Mode 
  bitset(CCP1CON, 3);
  bitset(CCP1CON, 2);

  TMR2ON = 1;
  while(1) {
    pcrlf();printf("Enter duty cycle (decimal, 0 to 255): ");
    pcrlf();
#if defined(HI_TECH_C)	
    scanf("%d",&dc_val);
#endif
#if defined(__18CXX)
    {// MCC18 does not have scanf, just get string from console, use atoi
	  char buf[30];
	  my_gets(buf);  // get a string from console
	  dc_val = atoi(buf);  // convert string to integer
	}
#endif
    pcrlf();printf("Setting duty cycle to %d.",dc_val);pcrlf();
    CCPR1L = dc_val;
  }
}




⌨️ 快捷键说明

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