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

📄 main.c

📁 基于micochip的dsPIC30F
💻 C
字号:
/*
; ?2005  Microchip Technology Inc.
;
; Microchip Technology Inc. (揗icrochip? licenses this software to you
; solely for use with Microchip dsPIC?digital signal controller
; products.  The software is owned by Microchip and is protected under
; applicable copyright laws.  All rights reserved.
;
; SOFTWARE IS PROVIDED 揂S IS.? MICROCHIP EXPRESSLY DISCLAIMS ANY
; WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
; PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP
; BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL
; DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR EQUIPMENT, COST OF
; PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
; BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
; ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS.
;
;
;FILE DESCRIPTION:
;  Please read the file named "README.txt", provided in this folder.
;  Please see detailed comments in this file, alongside source code.
;  Application code execution starts in this file.
;
;REVISION HISTORY:
;  $Log: Main.c,v $
;  Revision 1.1.1.1  2005/06/06 09:16:45  VasukiH
;  First release of software
;
;
*/

//Pre-Processor Directives:
//Provide pre-processor directives to include Device header files and
//any application specific header files. Path locations to the Device
//header files are set up within the MPLAB Project>>BuildOptions>>Project
//dialog box. The path should point to X:\\MPLAB C30\support\h\, where
//"X:" is the folder where the MPLAB C30 tools are installed.
#include <p30fxxxx.h>   //"p30fxxxx.h" is a generic header file for dsPIC30F
                        //devices. This file will in turn select the actual
                        //device header file, based on the device selected
                        //by the MPLAB workspace/project. Using the p30fxxxx.h
                        //file makes it easy to migrate a project from one
                        //dsPIC device to another.
#include "system.h"     //"system.h" is a header file defined for this demo
                        //application.
//Macros for Configuration Fuse Registers:
//Invoke macros to set up  device configuration fuse registers.
//The fuses will select the oscillator source, power-up timers, watch-dog
//timers, BOR characteristics etc. The macros are defined within the device
//header files. The configuration fuse registers reside in Flash memory.
_FOSC(CSW_FSCM_OFF & XT_PLL8);  //Run this project using an external crystal
                                //routed via the PLL in 8x multiplier mode
                                //For the 7.3728 MHz crystal we will derive a
                                //throughput of 7.3728e+6*8/4 = 14.74 MIPS(Fcy)
                                //,~67nanoseconds instruction cycle time(Tcy).
_FWDT(WDT_OFF);                 //Turn off the Watch-Dog Timer.
_FBORPOR(MCLR_EN & PWRT_OFF);   //Enable MCLR reset pin and turn off the
                                //power-up timers.
_FGS(CODE_PROT_OFF);            //Disable Code Protection

//Declaration to Link External Functions & Variables:
//Declare functions being used that are defined outside this file, or
//elsewhere in this MPLAB project.

extern INTx_Init(void);
extern Timer1_Init(void);
extern Timer2_Init(void);

//Functions and Variables with Global Scope:
//Declare functions in this file that have global scope.
//Define the 595 control bits
#define SH_CP LATBbits.LATB8
#define ST_CP LATBbits.LATB7
#define DS LATBbits.LATB6


void hc595_out1(char outdata2,char outdata1)       //the second 595 output value
{char j,outdata;
SH_CP=1;ST_CP=1;DS=1;
 outdata=outdata1;
 for(j=0;j<8;j++) { if((outdata&0x80)==0x80) {DS=1;}else{DS=0;}  // 串行口输入595;
Delay5us(1);
// delay_us(1);
SH_CP=0;//Delay5ms(1);
          Delay5us(1);
SH_CP=1;//Delay5ms(1); 
          Delay5us(1);
 outdata=outdata<<1;}                                              //左移1位;
 outdata=outdata2;
 for(j=0;j<8;j++) { if((outdata&0x80)==0x80) {DS=1;}else{DS=0;}
Delay5us(2);// delay_us(1);
SH_CP=0; //Delay5ms(1);
         Delay5us(1);
SH_CP=1; //Delay5ms(1);
         Delay5us(1);
 outdata=outdata<<1;} 
 ST_CP=0; //Delay5ms(1);
          Delay5us(1);
  ST_CP=1;//Delay5ms(1);
          Delay5us(1); 
  ST_CP=1;Delay5us(1);
 } 

// the LCD control function
unsigned char LCD_Console=0,LCD_DATA;
#define LCD_ENABLE 0x08   
#define RS 0x02
#define RW 0x04 



void lcd_checkbusy(void)
{  Delay5us(5000); }
   
void lcd_writecom(unsigned char combyte)
{ lcd_checkbusy();
//LCD_DirConsole|=RS;LCD_DirConsole|=RW;LCD_DirConsole|=LCD_ENABLE;
 LCD_Console&=~LCD_ENABLE;
 hc595_out1(LCD_DATA,LCD_Console);
 LCD_Console&=~RS;
 hc595_out1(LCD_DATA,LCD_Console);
 LCD_Console&=~RW;
 hc595_out1(LCD_DATA,LCD_Console);
 LCD_Console|=LCD_ENABLE; 
 hc595_out1(LCD_DATA,LCD_Console);
 //LCD_DirData=0x0FF; LCD_DataOut=combyte;
 LCD_DATA=combyte;
 hc595_out1(LCD_DATA,LCD_Console);
// hc595_out(combyte);
 LCD_Console&=~LCD_ENABLE;
 hc595_out1(LCD_DATA,LCD_Console);}
   
void lcd_writedata(unsigned char lcddata)
{lcd_checkbusy(); 
 //LCD_DirConsole|=RS;LCD_DirConsole|=RW;LCD_DirConsole|=LCD_ENABLE;
 LCD_Console&=~LCD_ENABLE;
 hc595_out1(LCD_DATA,LCD_Console);
 LCD_Console|=RS; 
 hc595_out1(LCD_DATA,LCD_Console);
 LCD_Console&=~RW;
 hc595_out1(LCD_DATA,LCD_Console);
 LCD_Console|=LCD_ENABLE;
 hc595_out1(LCD_DATA,LCD_Console);
 // hc595_out(lcddata); 
 LCD_DATA=lcddata;
 hc595_out1(LCD_DATA,LCD_Console);
 LCD_Console&=~LCD_ENABLE;  
 hc595_out1(LCD_DATA,LCD_Console);
 }
  
void lcd_initial(void)
{ lcd_writecom(0x38);lcd_writecom(0x0c);  lcd_writecom(0x06);}  
 
void lcd_clear(void)
{ lcd_writecom(0x01);lcd_writecom(0x02);}

void lcd_gotoxy(unsigned char x,unsigned char y)
{unsigned char base_y[2]={0x80,0xc0};  
 lcd_checkbusy(); lcd_writecom(base_y[y]+x); }

void lcd_dispdata(unsigned int data) 
{unsigned char datas,datahh,datal,datah,datam;
datas=data/10000+0x30;data=data%10000;
datahh=data/1000+0x30;data=data%1000;datah=data/100+0x30;datam=(data%100)/10+0x30;datal=(data%100)%10+0x30;
lcd_writedata(datas);lcd_writedata(datahh);lcd_writedata(datah);lcd_writedata(datam);lcd_writedata(datal);}    
  
void lcd_putsf(unsigned char *dispstr)
{ unsigned char i,strlenth=0;for(i=0;i<strlen(dispstr);i++){lcd_writedata(dispstr[i]);}}
//{ unsigned char i; i=0;while(dispstr[i]!=';'){lcd_writedata(dispstr[i]);i++;}}

////////////////finish lcd function////////////////////
////////////////QEI Init///////////////////////////////
void InitQEI(void)
{
ADPCFG |= 0x0030; // 将QEI 引脚配置为数字输入
QEICONbits.QEIM = 0; // 禁止QEI 模块
QEICONbits.CNTERR = 0; // 清除任何计数错误
QEICONbits.QEISIDL = 0; // 休眠期间继续工作
QEICONbits.SWPAB = 0; // QEA 和QEB 不交换
DFLTCONbits.CEID = 1; // 禁止计数错误中断
DFLTCONbits.QEOUT = 1; // 对于QEn 引脚,使能数字滤波器输出
DFLTCONbits.QECK = 5; // 将QEn 的数字滤波器设置为1:64 时钟分频
POSCNT = 0; // 复位位置计数器
MAXCNT=0xFFFF;//最大寄存器置最大值
QEICONbits.QEIM =7; // X4 模式
return;
}
////////////////////////////////////////全局变量////////
//volatile float MOTORSPEED;
/////////////////////////////////////////////////////////
int main (void);

//Code execution automatically reaches the main() function after
//two events have occurred;
//1. A Reset event triggered by hardware or software
//2. The execution of the C Start up library functions, present
//   in the crt0.o file in the libpic30-coff.a library file
int main (void)
{
        

        //Function Delay5ms() available in file, Delay.s
        Delay5us(10000);          //Provide 500ms delay for the LCD to start-up.

       
        InitQEI();             //initialize QEI
        //Function INTx_IO_Init() available in file, INTx_IO_pins.c
        INTx_IO_Init();         //Initialize the External interrupt pins and
                                //some I/O pins to accept input from the
                                //switches, S5 and S6 and drive the LEDs, D3
                                //and D4.

        //Function Timer1_Init() & Timer2_Init() available in file, Timers.c
        Timer1_Init();          //Initialize Timer1 to provide "blinking" time
                              //for the LEDs.
        Timer2_Init();          //Initialize Timer2 and Timer3 as a 32-bit
                                //Timer to be used for updating data sent to
                                //the LCD(SPI) and COM(UART) interfaces.
        T1CONbits.TON=0;
        lcd_initial();
        lcd_clear();
        lcd_gotoxy(0,0);
        lcd_putsf("motor speed");
        T1CONbits.TON=1;   
        
        
        
        

        while (1)               //Main Loop of Code Executes forever
        {Delay5us(1);
        }
        return 0;               //Code never reaches here!
}

/*
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                                                                    ;
;                       Software License Agreement                   ;
;                                                                    ;
;   The software supplied herewith by Microchip Technology           ;
;   Incorporated (the "Company") for its dsPIC controller            ;
;   is intended and supplied to you, the Company's customer,         ;
;   for use solely and exclusively on Microchip dsPIC                ;
;   products. The software is owned by the Company and/or its        ;
;   supplier, and is protected under applicable copyright laws. All  ;
;   rights are reserved. Any use in violation of the foregoing       ;
;   restrictions may subject the user to criminal sanctions under    ;
;   applicable laws, as well as to civil liability for the breach of ;
;   the terms and conditions of this license.                        ;
;                                                                    ;
;   THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION.  NO           ;
;   WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING,    ;
;   BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND    ;
;   FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE     ;
;   COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,  ;
;   INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.  ;
;                                                                    ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
*/

⌨️ 快捷键说明

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