/* Automates setup of V25 on-chip baud-rate generator. Used like this: #define BAUDSET_XTAL 16e+6 #define BAUDSET_DESIRED 9600ul #include // or #include "baudset.h" ... pokeb(PORT_SEG, SCC0, BAUDSET_SCC); pokeb(PORT_SEG, BRG0, BAUDSET_BRG); */ #if !defined(__BAUDSET_H) #define __BAUDSET_H #pragma warn -dup /* suppress warnings about re-defined macros (Borland) */ /* compute the value of the divisor (BRG register) if prescaled clock is BAUDSET_XTAL / 4 (SCC register =0) */ #define BAUDSET_SCALE 4 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 0 #else /* if divisor is too large, adjust prescale up and try again... */ #define BAUDSET_SCALE 8 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 1 #else /* ...and again... */ #define BAUDSET_SCALE 16 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 2 #else /* ...and again... */ #define BAUDSET_SCALE 32 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 3 #else /* (you get the idea) */ #define BAUDSET_SCALE 64 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 4 #else #define BAUDSET_SCALE 128 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 5 #else #define BAUDSET_SCALE 256 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 6 #else #define BAUDSET_SCALE 512 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 7 #else #define BAUDSET_SCALE 1024 #define BAUDSET_BRG (int)(BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_DESIRED + 0.5) #if BAUDSET_BRG<2 /* baud rate too high */ #error Desired baud can't be achieved with this crystal #elif BAUDSET_BRG<256 /* baud rate OK */ #define BAUDSET_SCC 8 #else #error Desired baud can't be achieved with this crystal #endif #endif #endif #endif #endif #endif #endif #endif #endif /* one final check to see if actual baud rate is close enough to desired */ #define BAUDSET_ACTUAL (BAUDSET_XTAL / BAUDSET_SCALE / BAUDSET_BRG) #define BAUDSET_ERR (BAUDSET_ACTUAL - BAUDSET_DESIRED) / BAUDSET_DESIRED #if (BAUDSET_ERR>0.05)||(BAUDSET_ERR<-0.05) #error Actual baud rate differs from desired > 5% #endif #endif /* __BAUDSET_H */