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

📄 basic.c

📁 一位十年经验美国工程师的工程代码
💻 C
字号:
//****************************************************************************
//                            basic.c
//                             V1.00
//
// This runs a simple serial control routine on a PIC 16F84 to drive the
// ports and 1-wire bus functions on the Demo Board.
// A serial combined command/data byte is sent from a PC which is translated
// into a read or write command. If a read command the demo board reads and
// returns data from the selected device. If a write command then the data is
// written to the selected port.
//
// Port assignments on the demo board are:
// RB0 / INT -> RXD - serial data receive
// RB1       -> TXD - serial data transmit
// RB2       -> 1-wire interface - temperature reading - 1 device only
// RB3 - RB7 -> 5 bit I/O port - connected to on board relays (active low)
// RA0 - RA4 -> 5 bit I/O port
//
//
// Target Processor: PIC 16F84
//
// Author: Michael Pearce
//         Chemistry Department, University of Canterbury
//
// Started: Monday 3 May 1999
//
//****************************************************************************
//                        Version Information
//****************************************************************************
// Version 1.00 - 03 May 1999
//  Start of the project.
//****************************************************************************
#define PIC_XTAL 8
#define PIC_CLK  8

#include <pic.h>
#include "always.h"            //-- Shanes Always include this header file
#include "int232.c"         //-- Mikes Interrupt driven serial routines
//#include "1wire.c"             //-- Mike 1-wire bus routines

#include "commands.h"


char Temperature=123;

void InitPorts(void);
void DoCommand(char cmd);

//****************************************************************************
//                           main
//****************************************************************************
void main(void)
{
 InitPorts();
 InitUart();
 while(1)
 {
  DoCommand(getch());
 }
}
//************** END OF main

//****************************************************************************
//                        DoCommand
//****************************************************************************
void DoCommand(char cmd)
{
 switch(cmd & 0xE0)     //-- Look at the command only
 {
  case ReadPortA:
   cmd=PORTA;           //-- Read Port A Pins
   cmd &=0x1F;          //-- Clear the top 3 Bits
   cmd |=ReadPortA;     //-- Tell PC what the command was (just in case)
   putch(cmd);          //-- Send the command to the PC
   break;

  case ReadPortB:
   cmd=PORTB;           //-- Read Port A Pins
   cmd=cmd>>3;          //-- Shift port data to bottom 5 bits
   cmd &=0x1F;          //-- Clear the top 3 Bits
   cmd |=ReadPortB;     //-- Tell PC what the command was (just in case)
   putch(cmd);          //-- Send the command to the PC

   break;

  case Read1wire:
   //Temperature=GetTemperature(); //-- Read the temperature from the 1-wire Probe

   //-- Send the temperature back as two nibbles - indicated by bit 4.
   cmd = Temperature &0x0F;    //-- Send Low Nibble
   cmd &=0xEF;                 //-- Indicate Low Nibble
   cmd |= Read1wire;           //-- Attach Command
   putch(cmd);                 //-- Send the data

   cmd = (Temperature >>4) & 0x0F;    //-- Send High Nibble
   cmd |= 0x10;                //-- Indicate that it is High Nibble
   cmd |= Read1wire;           //-- Attach Command
   putch(cmd);                 //-- Send the data
   break;

  case ReadADC:
   //-- Not valid for the PIC16F84 so return 0
   putch(ReadADC);
   break;

  case WritePortA:
   cmd &=0x1F;                 //-- remove command from the data
   PORTA=cmd;                  //-- Write data to the port
   cmd=PORTA &0x1F;            //-- Read in data from port
   cmd |=WritePortA;           //-- Add Command
   putch(cmd);                 //-- Reply with current output status
   break;

  case WritePortB:
   cmd &=0x1F;                 //-- remove command from the data
   cmd = cmd <<3;              //-- Shift data to correct position
   cmd &= 0xF8;                //-- Ensure bottom 3 bits clear
   cmd |= (PORTB & 0x07);      //-- read and add current RXD/TXD/1-wire states
   PORTB=cmd;                  //-- Write data to the port
   cmd=PORTB & 0xF8;           //-- Read in data from port
   cmd=cmd >> 3;               //-- Shift data to correct position
   cmd &= 0x1F;                //-- Ensure command bits clear
   cmd |=WritePortA;           //-- Add Command
   putch(cmd);                 //-- Reply with current output status
   break;

  case WriteTrisA:
   cmd &=0x1F;                 //-- Remove the command
   TRISA = cmd;                //-- Write the tris register
   cmd = TRISA & 0x1F;         //-- Read the tris Register
   cmd |=WriteTrisA;           //-- Add the Command to data
   putch(cmd);                 //-- Return what was written

   break;

  case WriteTrisB:
   cmd &=0x1F;                 //-- Remove the command
   cmd = cmd << 3;             //-- Shift to correct position
   cmd &= 0xF8;                //-- Clear bottom 3 bits
   cmd |= (TRISB & 0x07);      //-- Read and add RXD/TXD/1-wire states
   TRISB=cmd;                  //-- Write the TRIS
   cmd=TRISB;                  //-- Get current TRIS values
   cmd=cmd >>3;                //-- Shift to correct position
   cmd &=0x1F;                 //-- Clear command Bits
   cmd |=WriteTrisB;           //-- Add teh command
   putch(cmd);                 //-- Return what was written
   break;
 }
}
//************** END OF DoCommand

//****************************************************************************
//                       InitPorts
//****************************************************************************
void InitPorts(void)
{
 TRISA=0xFF;                    //-- Port A to input state
 TRISB=0xFF;                    //-- Port B to input state
 PORTA=0x00;                    //-- Port A pins initially Low
 PORTB=0xFF;                    //-- Port B pins initially High (Due to Relays)
}
//************** END OF InitPorts

//****************************************************************************
//
//****************************************************************************


//************** END OF

//****************************************************************************
//
//****************************************************************************


//************** END OF

//****************************************************************************
//
//****************************************************************************


//************** END OF

//****************************************************************************
//
//****************************************************************************


//************** END OF







⌨️ 快捷键说明

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