dvsoft
Joined: 28 Nov 2003 Posts: 46
|
MAX197 Driver PIC18F452 |
Posted: Mon Aug 02, 2004 5:40 pm |
|
|
bonjour,
My driver for the MAX197
Code: |
//---------------------------------------------------------------------------
// Copyright (c) DVSoft 2004
// email [email protected]
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation.
//
// This program is distributed in the hope it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANDIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// Originale Unit Name : Max197Driver.c
// Author : Alain
// Date : 15-Avril-2004
// Project : YA1-LOGGER-V3
// Version : 0.9.0.1
// Revision :
//
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Conditional PinDef
//
#ifndef __MAX197_PIN_DEF__
#define MAX197_INT PIN_B0 // I 1
#define MAX197_HBEN PIN_B2 // O 0
#define MAX197_RD PIN_B3 // O 0
#define MAX197_WR PIN_B4 // O 0
#define MAX197_CS PIN_B5 // O 0
#define MAX197_D0 PIN_D0 // I/O 1/0
#define MAX197_D1 PIN_D1 // I/O 1/0
#define MAX197_D2 PIN_D2 // I/O 1/0
#define MAX197_D3 PIN_D3 // I/O 1/0
#define MAX197_D4 PIN_D4 // I/O 1/0
#define MAX197_D5 PIN_D5 // I/O 1/0
#define MAX197_D6 PIN_D6 // I/O 1/0
#define MAX197_D7 PIN_D7 // I/O 1/0
//------------------------------------------------------------------------
// tris Def
#define TRIS_D_IN 0b11111111
#define TRIS_D_OUT 0b00000000
#define TRIS_B 0b00000011
//------------------------------------------------------------------------
// Fast IO
//
#use fast_io(B)
#use fast_io(D)
#endif
//----------------------------------------------------------------------------
// Max197 Data
//
#ifdef __YA1_LOGGER_DATA_REGISTER__
//------------------------------------------------------------------------
// Max197 driver status
//
BOOLEAN Max197DriverRun;
//------------------------------------------------------------------------
// Max197 Data Registers
//
int16 AN0Raw; // AN0Raw
#byte AN0Raw_L = AN0Raw
#byte AN0Raw_H = AN0Raw+1
int16 AN1Raw; // AN1Raw
#byte AN1Raw_L = AN1Raw
#byte AN1Raw_H = AN1Raw+1
int16 AN2Raw; // AN2Raw
#byte AN2Raw_L = AN2Raw
#byte AN2Raw_H = AN2Raw+1
int16 AN3Raw; // AN3Raw
#byte AN3Raw_L = AN3Raw
#byte AN3Raw_H = AN3Raw+1
int16 AN4Raw; // AN4Raw
#byte AN4Raw_L = AN4Raw
#byte AN4Raw_H = AN4Raw+1
int16 AN5Raw; // AN5Raw
#byte AN5Raw_L = AN5Raw
#byte AN5Raw_H = AN5Raw+1
int16 AN6Raw; // AN6Raw
#byte AN6Raw_L = AN6Raw
#byte AN6Raw_H = AN6Raw+1
int16 AN7Raw; // AN7Raw
#byte AN7Raw_L = AN7Raw
#byte AN7Raw_H = AN7Raw+1
#endif
//----------------------------------------------------------------------------
// Max197 Macro
//
#define Max197Select() output_low(MAX197_CS)
#define Max197UnSelect() output_high(MAX197_CS)
#define Max197_RDLow() output_low(MAX197_HBEN)
#define Max197_RDHigh() output_high(MAX197_HBEN)
#define Max197RD_On() output_low(MAX197_RD)
#define Max197RD_Off() output_high(MAX197_RD)
#define Max197WR_On() output_low(MAX197_WR)
#define Max197WR_Off() output_high(MAX197_WR)
#define Max197EnableInt() enable_interrupts(INT_EXT)
#define Max197DisableInt() disable_interrupts(INT_EXT)
//----------------------------------------------------------------------------
// Max197 Local Data
//
BOOLEAN Max197EOC; // Wait for end of conversion
BOOLEAN Max197Pending; // Convertion running
int16 Max197Row; // 16 bits Value
#byte Low = Max197Row // Max197 LSB
#byte High = Max197Row+1 // Max197 MSB
int8 ChannelCnt; // Current channel
int8 ANShiftBit; // Current channel Shift Bit Enabled/Disable
//----------------------------------------------------------------------------
// Current test bit channel
//
int8 ANMaskBitChannels;
#bit AN0MaskBitEnabled = ANMaskBitChannels.0
#bit AN1MaskBitEnabled = ANMaskBitChannels.1
#bit AN2MaskBitEnabled = ANMaskBitChannels.2
#bit AN3MaskBitEnabled = ANMaskBitChannels.3
#bit AN4MaskBitEnabled = ANMaskBitChannels.4
#bit AN5MaskBitEnabled = ANMaskBitChannels.5
#bit AN6MaskBitEnabled = ANMaskBitChannels.6
#bit AN7MaskBitEnabled = ANMaskBitChannels.7
//---------------------------------------------------------------------------
// Function : NextChannel()
// Input :
// void
// return :
// void
// Modify :
// Set the next channel bit
//
// Note :
// Set the first channel if Current Channel+1 > 7
#inline
void NextChannel(void)
{
//--- Next Channel
ChannelCnt++;
//--- Shift the enabled bit
ANShiftBit >>= 1;
//--- Enabled next Channels
ANMaskBitChannels = (ANMaskBitChannels << 1) & 0xff;
//--- Last Channel ? (0 to 7)
if (ChannelCnt > 7) {
//--- First Channel
ChannelCnt = 0;
//--- First channel enabled bit
ANMaskBitChannels = 0x01;
//--- Enabled channels bits
ANShiftBit = EnabledChannels;
}// End If
}
//---------------------------------------------------------------------------
// Function : Max197Read(void)
// Input :
// void
// return :
// void
// Modify :
// Store the MAX197 value in Low an High
//
// Note :
// Don't forget the waitstate befort read the MAX197 DataBus
//
#inline
void Max197Read(void)
{
//--- Port D read
set_tris_d(TRIS_D_IN);
//--- Max197 Select
Max197Select();
//--- RD = 0
Max197RD_On();
//--- Wait 120 ns befort read (TD0)
delay_cycles(2);
//--- Store low Value
Low = input_d();
//--- HBEN = 1
Max197_RDHigh();
//--- Wait 120 ns befort read (TD1)
delay_cycles(2);
High = input_d();
//--- HBEN = 0
Max197_RDLow();
//--- RD = 1
Max197RD_Off();
//--- UnSelect
Max197UnSelect();
}
//---------------------------------------------------------------------------
// Function : Max197Open(void)
// Input :
// void
// return :
// void
// Modify :
// Init the MAX197 Data
// Set the Max197 driver flag 'Max197DriverRun' to TRUE
//
// Note :
//
//
#separate
void Max197Open(void)
{
BYTE i;
//--- Unselect
Max197UnSelect();
//--- Low Value First
Max197_RDLow();
//--- Signal Off
Max197RD_Off();
Max197WR_Off();
//--- Non convertion running
Max197Pending = FALSE;
//--- Empty the raw vale
AN0Raw = 0;
AN1Raw = 0;
AN2Raw = 0;
AN3Raw = 0;
AN4Raw = 0;
AN5Raw = 0;
AN6Raw = 0;
AN7Raw = 0;
//--- Loop index
ChannelCnt = 0;
//--- Current bit channel
ANMaskBitChannels = EnabledChannels;
//--- Driver status
Max197DriverRun = TRUE;
}
//---------------------------------------------------------------------------
// Function : Max197Close(void)
// Input :
// void
// return :
// void
// Modify :
// Set the Max197 driver flag 'Max197DriverRun' to FALSE
//
// Note :
//
//
#separate
void Max197Close(void)
{
//--- Driver stop
Max197DriverRun = FALSE;
//--- No interruption
Max197DisableInt();
}
//---------------------------------------------------------------------------
// Function : Max197Service(void)
// Input :
// void
// return :
// void
// Modify :
// Store the current channel value
// AN0Raw to AN7Raw
// Select the next channel
// Note :
//
// Usage in Main
//
// Max197Open();
// while (TRUE) {
// ....
// Max197Service();
// }
//
//
#inline
void Max197Service(void)
{
//--- Running ?
if (!Max197DriverRun)
return;
//--- Max197 Running convertion ?
if (Max197Pending) {
//--- End of convertion ?
if (!input(MAX197_INT)) {
Max197Read();
//--- Channel 0
if (AN0MaskBitEnabled) {
AN0Raw_H = High;
AN0Raw_L = Low;
}// End IF
//--- Channel 1
if (AN1MaskBitEnabled) {
AN1Raw_H = High;
AN1Raw_L = Low;
}// End IF
//--- Channel 2
if (AN2MaskBitEnabled) {
AN2Raw_H = High;
AN2Raw_L = Low;
}// End IF
//--- Channel 3
if (AN3MaskBitEnabled) {
AN3Raw_H = High;
AN3Raw_L = Low;
}// End IF
//--- Channel 4
if (AN4MaskBitEnabled) {
AN4Raw_H = High;
AN4Raw_L = Low;
}// End IF
//--- Channel 5
if (AN5MaskBitEnabled) {
AN5Raw_H = High;
AN5Raw_L = Low;
}// End IF
//--- Channel 6
if (AN6MaskBitEnabled) {
AN6Raw_H = High;
AN6Raw_L = Low;
}// End IF
//--- Channel 7
if (AN7MaskBitEnabled) {
AN7Raw_H = High;
AN7Raw_L = Low;
}// End IF
//--- Max197 Stop
Max197Pending = FALSE;
//--- Next Channels
NextChannel();
}// End IF
}// End If
//--- Max197 free ??
else {
//--- Channel enabled ?
if (ANShiftBit & 0x01) {
//--- Port D write
set_tris_d(TRIS_D_OUT);
//--- Select the Max197
Max197Select();
//--- Write = 0
Max197WR_On();
//--- Configuration + Current Channel number
// 0 to 5 Volts
//
output_d(64+ChannelCnt);
//--- Write = 1
Max197WR_Off();
//--- Max197 UnSelect
Max197UnSelect();
//--- Convertion Started
Max197Pending = TRUE;
}// End IF
//--- No goto next channel
else
NextChannel();
}// End Else
}
//---------------------------------------------------------------------------
|
|
|